• 2 nd aggregate data type: struct • Recall: aggregate meaning "grouping" – Recall array: collection of values of same type – Structure: collection of values of different types • Treated
Trang 1Tin h c c s 3 ọ ơ ở
Structure
Trang 2Outline
Trang 3• 2 nd aggregate data type: struct
• Recall: aggregate meaning "grouping"
– Recall array: collection of values of same type – Structure: collection of values of different types
• Treated as a single item, like arrays
• Major difference: Must first "define"
struct
– Prior to declaring any variables
Trang 4– Heterogeneous (components are different types) – Fixed-size (well-defined set of components)
Trang 5Structured Data Types
variables, perhaps of different
types, grouped together under a
Trang 6Structured Data Types
– Just a “placeholder” for what struct will “look like”
– Name of struct
– Name of each field
– Type of each field (could be another
record/struct)
Trang 7Defining Struct
• Defining a new data type using struct
struct date name of new "type"
struct date yesterday;
• Once defined, this new data type could be used as other data types:
–Variable of this type could be assigned to another variable of the same type
–Variable of this type could be passed as a parameter to a function
Trang 8Declare Structure Variable
variables of this new type:
struct date today;
– Just like declaring simple types
– Variable today now of type struct date
– It contains "member values"
• Each of the struct "parts"
Trang 9Accessing Structure Members
• Dot Operator to access members
– today.day
– today.month
– today.year
• Called "member variables"
– The "parts" of the structure variable
– Different structs can have same name member
variables
• No conflicts
Trang 10Defining New Type Struct
struct
typedef struct date Date;
basic type without the need of
Trang 11Structure Example
void printData(Date today)
{
printf("The day is: %i", today.day);
printf("The month is: %i", today.month); printf("The year is: %i", today.year); }
void getData(Date *today);
Trang 13Structure Assignments
• Given structure named Fruit
• Declare two structure variables:
Fruit apples, oranges;
– Both are variables of type "Fruit"
– Simple assignments are legal:
apples = oranges;
• Simply copies each member variable from apples
into member variables from oranges
Trang 14Structure Other Operations
• Operations which are NOT defined for struct type:
– compare for equality/inequality
(i.e apples == oranges is not a legal expression)
– compare based on ordering (<, >, )
(i.e apples < oranges is not a legal expression)
– arithmetic operations
(i.e apples + oranges is not a legal expression)
– reading from or writing to text files
(i.e no printf(apples) no scanf(&oranges))
• If you need such operations, write your own functions.
Trang 15Structure and Pointer
– (struct x)* is a pointer to struct x
– & gives the address of a structure
binds more tightly than *
field of the struct:
– *p.a means *(p.a) which is ILLEGAL.
– You must use (*p).a
– For convenience , the operator -> combines indirection and
component selection.
– p->a is equivalent to (*p).a
Trang 16Structure and Pointer
ap->x = 0; /* right */
Trang 17Structure and Pointer
void getData(Date *today)
Trang 18Structure as Function Argument
– Pass-by-value – Pass-by-reference – Or combination
– Return-type is structure type – Return statement in function definition sends structure variable back to caller
Trang 19Other Type Definition
• We can use the keyword typedef to
make our own definitions:
typedef float Floating;
• This means variables can be declared
as Floating but they will actually be
of type float.
• If we later decide we need more
precision, we can change to:
typedef double Floating;
without changing the codes
Trang 20Combining struct and typedef
struct employee { char name[30];
• Note: we use the convention that the name of the
defined type is the same as the struct modifier,
but with the first letter capitalized.
Trang 21Self-referential types
• A very powerful programming technique
is to create struct with fields which contain a reference to an object in the same struct For example:
Trang 22Linked Lists
• Consider the following data
structure:
– A list that grow over time
– Items are added one by one
– Each item is a number
• It might grow like this:
Trang 23Linked Lists
• How do you implement such a list in C++?
• You can use an array but you must check the array doesn't overflow and allocate memory for a new larger array if the array fills.
• Can be implemented using a self-referential type.
• Each element in the linked list need:
– some information (the data)
– a connection to the next item
• The individual elements in data structures like this are often called nodes.
Trang 24struct list_node *list_start;
The next field of the struct will be NULL.
Trang 25Linked Lists