Tin h c c s 3ọ ơ ở
Structure
Outline
•
Structured Data types
•
Structures as function arguments
•
Self-referential types: linked list
Lê Nguyên Khôi 2
Structure
•
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
Lê Nguyên Khôi 3
Structure
•
Sometimes referred as records.
Lê Nguyên Khôi 5
Structured Data Types
•
Define struct globally (typically)
•
No memory is allocated
–
Just a “placeholder” for what struct will “look like”
•
To define struct, we must give:
–
Name of struct
–
Name of each field
–
Type of each field (could be another
record/struct)
Lê Nguyên Khôi 6
Defining Struct
•
Defining a new data type using struct
struct date name of new "type"
{
int day; member names
int month;
int year;
}; “;” after “}”
struct date yesterday;
•
today.year
•
Called "member variables"
–
The "parts" of the structure variable
–
Different structs can have same name member
variables
•
No conflicts
Lê Nguyên Khôi 9
Defining New Type Struct
•
Could use typedef to define a new type
struct
typedef struct date Date;
•
And then we could use Date as other
basic type without the need of
including struct
Date today;
•
Compare to previous declaration of
yesterday
struct date yesterday;
Lê Nguyên Khôi 10
Structure Example
void printData(Date today)
{
printf("The day is: %i", today.day);
Simple assignments are legal:
apples = oranges;
•
Simply copies each member variable from apples
into member variables from oranges
Lê Nguyên Khôi 13
Structure 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.
Lê Nguyên Khôi 14
Structure and Pointer
•
As for other types:
–
Lê Nguyên Khôi 16
Structure and Pointer
void getData(Date *today)
{
printf("Enter day: ");
scanf("%i", &((*today).day));
printf("Enter month: ");
scanf("%i", &(today->month));
printf("Enter year: ");
scanf("%i", &today->year);
}
Lê Nguyên Khôi
Structure as Function Argument
•
Passed like any simple data type
–
Pass-by-value
–
Pass-by-reference
–
Or combination
•
Can also be returned by function
–
Return-type is structure type
–
Return statement in function definition
sends structure variable back to caller
Lê Nguyên Khôi 18
Other Type Definition
A very powerful programming technique
is to create struct with fields which
contain a reference to an object in
the same struct. For example:
struct list_node {
int data;
struct list_node *next;
};
•
This approach can be used to create
some very useful data structures.
Lê Nguyên Khôi 21
Linked 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:
Lê Nguyên Khôi 22
Linked Lists
•
How do you implement such a list in C++?
•
You can use an array but you must check the
Lê Nguyên Khôi 24
Linked Lists
Lê Nguyên Khôi 25