ESC151LectureNotes Structures
ESC151LectureNotes Structures
in C Programming
ESC 151
Sunnie Chung
Data Structures: Struct
• A Structure is a collection of related data items, possibly of different
types.
• A structure type in C/C++ is called Struct.
• A Struct is heterogeneous in that it can be composed of data of
different types.
• In contrast, array is homogeneous since it can contain only data of the
same type.
4
Different types of data in each row structure
in Table Employee
Employee
makes the name Length a synonym (or alias) for the data type int.
• The data “type” name Length can now be used in declarations in exactly
the same way that the data type int can be used:
Length a, b, len ;
Length numbers[10] ;
ESC 151 C Programming Sunnie Chung 8
Declaring with Typedef & Struct : better way
• Often, typedef is used in combination with struct to declare a synonym (or
an alias) for a structure:
js.person.id = 123456789 ;
js.person.gpa = 3.4 ;
printf ("%s %ld %f\n", js.name, js.person.id, js.person.gpa) ;
printf ("%s %ld %f\n", jsptr->name, jsptr->person.id, jsptr->person.gpa) ;
}
• The tag_name is not used directly. The names in the braces are symbolic constants that
take on integer values from zero through n. As an example, the statement:
enum colors { red, yellow, green } ;
• creates three constants. red is assigned the value 0, yellow is assigned 1 and green is
assigned 2.