by: Lizawati, Norhidayah & Muhammad Noorazlan Shah Computer Engineering, FKEKK, UTeM
At the end of this chapter, the students should be able to: understand and apply typedef understand and apply structure (struct) understand and apply Enumeration (enum) understand and apply union
User Define Data Type (typedef) Structured Type definition, initialization, assignment pointer to structures nested structures arrays of structures structures and functions Enumerated Type Union Type
Derived Types Arrays Pointers Structure Enumerated Union Chapter 5 Chapter 7
creating synonyms for previously defined data type names is defined using the keyword typedef and the syntax is: typedef daya_type variable_name;
Example: typedef int LENGTH; /*LENGTH is a synonym for the data type int*/ LENGTH a, b, len; LENGTH numbers[10];
Structure is a collection of variables that are functionally related to each other group of related values the group is treated as a single unit each member of structure is a field each field of the structure has a specific type different fields may have either the same or different types
Group Field1 Field2 Field3 Field4 Field 1 Field 2 id name CGPA Field 3 student Structure
Declaration & definition: keyword struct Most common declaration method describe as follows: 1. Declaring a tag name and then using the tag name to declare actual variables This form is more commonly used, especially for a large program struct tagname type 1 fieldname 1 ; type 2 fieldname 2 ; type n fieldname n ; }; struct tagname varname; struct STUDENT int id; char name[30]; float CGPA; }; struct STUDENT benc, bene; benc & bene is declared as variable of type struct STUDENT with 3 members, id, name and CGPA
2. Declaring a structure without a tag name This form is useful if the structure is used only in one place struct type 1 fieldname 1 ; type 2 fieldname 2 ; type n fieldname n ; } varname; struct int id; char name[30]; float CGPA; }benc, bene;
3. Declaring a structure with a tag name and variables struct tagname type 1 fieldname 1 ; type 2 fieldname 2 ; type n fieldname n ; } varname; struct STUDENT int id; char name[30]; float CGPA; }benc, bene; benc & bene is declared as variable of type struct STUDENT
typedef of structure declaration & definition: typedef struct tagname type 1 fieldname 1 ; type 2 fieldname 2 ; type n fieldname n ; } typename; typename varname; typedef struct STUDENT int id; char name[30]; float CGPA; } stud; stud benc, bene; Create a data type call stud. benc & bene is declared as variable of typedef stud. Keyword struct can be ignore since stud is not normal structure
Initialization: struct tagname varname = fieldval 1, fieldval 2,..., fieldval n }; struct STUDENT char id[8]; char name[30]; float CGPA; }; struct STUDENT stud1= 0123, Anne, 3.98}; 0123 Anne 3.98 id name CGPA struct STUDENT stud2= 0123 }; 0123 id \0 name 0.0 CGPA
Accessing Structures: dot (.) form to refer to field of a structure each field treated as an individual variable varname.fieldname typedef struct DATE int month, day, year; } date; month d1 day year void main() date d1; 12 2 1970 d1.month = 12; d1.day = 2; d1.year = 1970; }
Structure Assignment: variables must be of same type (same name) values are copied one at a time from field to corresponding field typedef struct DATE int month, day, year; } date; month 12 d1 day 2 year 1970 date d1 = 12, 2, 1970 }; date d2; d2 = d1; /* Assignment */ d2.day = d1.day; month 12 d2 day 2 year 1970
used to reference structures can only stored the address of a structure struct STUDENT char id[8]; char name[30]; float CGPA; }; struct STUDENT stud1 = 0123, Anne, 3.98}; struct STUDENT *ptr; //declare a pointer ptr = &stud1;//assign address of stud1 ptr 0123 Anne name 3.98 id name CGPA
Accessing Pointer Structures: (*pointername). fieldname Or pointername -> fieldname printf ( Student CGPA : %.2f, stud1.cgpa); or printf ( Student CGPA : %.2f, (*ptr).cgpa); or printf ( Student CGPA : %.2f, ptr->cgpa);
Example: a group of students and the data stored in a structure Create array of structure: struct STUDENT stuary[30]; Initialize for two students: stuary = 1001, Ahmad, 2.98}, 1002, Siti, 3.01}};
Access data for a student: refer to the structure name with an index or a pointer stuary[i] or *pstu
Example: compute CGPA of all students float totcgpa; float avrcgpa; int i; struct STUDENT stuary[30]; accessing using index for (i=0; i<30; i++) totcgpa += stuary[i].cgpa; avrcgpa = totcgpa/30; accessing using pointer float totcgpa; float avrcgpa; struct STUDENT *pstu, *plaststu; plaststu = stuary + 29; for (pstu=stuary; pstu<=plaststu; pstu++) totcgpa += (*pstu).cgpa; avrcgpa = totcgpa/30;
a structure includes another structure stamp.date.month stamp.time.min month day year hour min sec date time stamp.date stamp stamp.time struct DATE int day; int month; int year; }; struct TIME int hour; int min; int sec; }; struct STAMP struct DATE date; struct TIME time; }; struct STAMP stamp;
Accessing nested structures include each level from the highest to the component being referenced stamp stamp.date stamp.date.month stamp.time stamp.time.sec
Nested structure initialization: each structure must be initialized completely before proceeding to the next member each structure is enclosed in a set of braces ( }) struct STAMP stamp = 12, 06, 1983}, 23, 35, 05}};
Two methods to pass structures as parameters of a function: Passing by value passing a copy of the entire structure suitable if the structure is very small to make sure the called function does not change the structure being passed Passing by reference passing the address of the structure faster and more convenient method
#include <stdio.h> struct STUDENT char id[8]; char name[30]; float CGPA; }stud1 = "0123", "Anne", 3.98}; void func(struct STUDENT s, struct STUDENT *sp) printf("s.id = %s\n", s.id); printf("s.name = %s\n", s.name); printf("s.cgpa = %f\n", s.cgpa); } printf("sp->id = %s\n", sp->id); printf("sp->name = %s\n", sp->name); printf("sp->cgpa = %f\n", sp->cgpa);
int main() struct STUDENT stud2="0124", "Joe", 3.25}; } //passed structure stud1 and address of stud2 to func() func(stud1, &stud2); return 0;
The return type of the function must match the actual returned value One advantage of returning a structure is the ability to return more than one value However, function passing as reference is more commonly used than functions returning structures
#include <stdio.h> #include <string.h> struct STUDENT int id; char name[30]; float CGPA; }; /* function returns a structure */ struct STUDENT func(int id) struct STUDENT s1; s1.id = id + 1; strcpy(s1.name, "Anne"); //copy string Anne to s1.name s1.cgpa = 2.98; } return s1;
int main() struct STUDENT stud1=1000, "Joe", 3.25}, stud2; stud2 = func(stud1.id); //assign the returned structure to stud2 printf("stud2.id = %d\n", stud1.id); printf("stud2.name = %s\n", stud2.name); printf("stud2.cgpa = %f\n", stud2.cgpa); } return 0;
Enumerated types: is derived from the integer type is defined using the keyword enum and the syntax is: enum tag_name name_0, name_1,, name_n }; the names in an enumerated type are replaced by integer values by the compiler
the values in an enum start with 0, unless specified otherwise, and are incremented by 1. enum days Mon, Tue, Wed, Thu, Fri, Sat, Sun }; Mon is 0, Tue is 1, Wed is 2, etc give different initial value enum days Mon = 1, Tue, Wed, Thu, Fri, Sat, Sun }; Mon is 1, Tue is 2, Wed is 3, etc
give a value for each enum numbers First = 1, Third = 3, Fifth = 5}; Three thoughts about enumerated types: You can assign the same integer to multiple enumeration constants in the same definition enum colors green, blue, purple, yellow = 1}; assign same number to blue and yellow
You can assign an enumerated type to an integer enum colors green=2, blue, purple, yellow = 1}; enum colors shirt; int x; shirt = purple; x = purple; //OK. x is 4
Example 1: enum days Sun, Mon, Tue, Wed, Thu, Fri, Sat }; int day; for (day = Sun; day <= Sat; day++) if ((day == Sun) (day == Sat)) printf( Weekend\n ); else printf( Weekday\n ); } printf( %d\n, Wed); //Would print 3
Example 2: #include<stdio.h> int main() enum sport soccer=3, bowling=5, tenis, badminton}; enum sport game; game = badminton; } switch(game) case (3): printf("soccer"); break; case (5): printf("bowling"); break; case (6): printf("tenis"); break; case (7): printf("badminton"); break; } return 0;
Union: like a structure, a union is also a derived data type the members of a union share a single storage space only ONE member of each union can be referenced at a time amount of space allocated for storage is the amount needed for the largest member of the union
Union vs. Structure union NUMBER int x; float y; } union NUMBER val; struct NUMBER int x; float y; } struct NUMBER val; 4 byte storage (float) val x or y 6 byte storage (float + int) val x y
Example: #include<stdio.h> union number int x; float y; float z; }; void main() union number val; printf ("size of union is %d\n", sizeof(val)); //size of union is 4 val.x = 5; printf("x is %d\n", val.x); //x is 5
val.y = 100.25; printf("y is %.2f\n", val.y); //y is 100.25 val.z = 200.50; printf("z is %.2f\n", val.z); //z is 200.50 } printf("y is %.2f\n", val.y); //y is 200.50
Can you differentiate between enum, struct and union? apply enum, struct and union?