Presentation is loading. Please wait.

Presentation is loading. Please wait.

Structure A collection of values (members) struct date{ int day; char month[10]; int year; }; Declare a structure variable struct date today; struct struct_name.

Similar presentations


Presentation on theme: "Structure A collection of values (members) struct date{ int day; char month[10]; int year; }; Declare a structure variable struct date today; struct struct_name."— Presentation transcript:

1 Structure A collection of values (members) struct date{ int day; char month[10]; int year; }; Declare a structure variable struct date today; struct struct_name { type data_member1; type data_member2; …; type data_memberN; };

2 Why Structure To collect a set of variables together, and reference them as a unit. –Group different type of variables –Treat them as a single object

3 Declaration Three ways struct date{ int day; char month[10]; int year; }; struct date today; typedef struct { int day; char month[3]; int year; } date; date today; struct { int day; char month[3]; int year; } today;

4 Initialization struct { int day; char month[10]; int year; } today = {19, “ March ”, 2007}; typedef struct { int day; char month[4]; int year; } date; date today = {19, “March”, 2007}; struct date{ int day; char month[10]; int year; }; struct date today = {19, “March”, 2007};

5 How to use To access the members in the structure –specify the variable name, followed by a period and the member name today.day = 19; today.year = 2007; today.month[0]=‘M’; today.month[1]=‘a’; today.month[2]=‘r’; today.month[3]=‘c’; today.month[3]=‘h’; today.month[3]=‘\0’; 19 M a r c h \0 2007.month.day.year today

6 How to use structure – cont. Structure variable can be passed as a parameter to a function. Structure variable can be returned from a function Can have arrays of structures, and structures containing arrays or other structures. –struct date list_of_days[10]; –struct diary { int location; struct date when; char names[10]; };

7 Scope A structure type declaration can be local or global –if declared locally, it is only valid locally. Example: void main () { struct ONE { int a; float b; }; struct ONE x; } int f() { struct ONE x; }

8 union union is a collection of variables of different types Similar to a structure, except only one field of information can be stored at any time union union_name { type data_member1; type data_member2; …; type data_memberN; };

9 Example union length { int foot; int meter; }; union length obj; obj.foot = 3; obj.meter = 5; printf("The object size is %i foot ", obj.foot); printf("and %i meter \n.", obj.meter); The object size is 5 foot and 5 meter.

10 Comparing to Structure struct length { int foot; int meter; }; struct length obj; obj.foot = 3; obj.meter = 5; printf("The object size is %i foot ", obj.foot); printf("and %i meter \n.", obj.meter); The object size is 3 foot and 5 meter.

11 enum enum: enumerate –declare and initialize a sequence of integer constants enum colors {RED, YELLOW, GREEN, BLUE}; enum colors {RED=5, YELLOW, GREEN, BLUE}; Delaration –enum colors background; Assign value –background = RED;

12 Integer Value for Enum #include int main() { enum colors {RED, YELLOW, GREEN, BLUE}; printf("RED = %d\n", RED); printf("YELLOW = %d\n", YELLOW); printf("GREEN = %d\n", GREEN); printf("BLUE = %d\n", BLUE); return 0; } RED = 0 YELLOW = 1 GREEN = 2 BLUE = 3


Download ppt "Structure A collection of values (members) struct date{ int day; char month[10]; int year; }; Declare a structure variable struct date today; struct struct_name."

Similar presentations


Ads by Google