200 likes | 349 Views
Structures, Unions, and Enumerations. Structure Variables. The properties of a structure are quite different from those of an array. The elements of a structure aren’t required to have the same type. Declaring Structure Variables.
E N D
Structure Variables • The properties of a structure are quite different from those of an array. • The elements of a structure aren’t required to have the same type.
Declaring Structure Variables • It is logical to organize a collection of related data items into a structure. struct{ int number; char name[NAME_LEN+1]; int on_hand; } part1, part2; struct {….} specifies a type
Sturcture in Memory 2000 2001 number name on_hand
Initializing Structure Variables struct{ int number; char name[NAME_LEN+1]; int on_hand; } part1 = {528, “Disk drive”, 10}, part2 = {914, “printer cables”, 5};
Operations on Structure printf(“part number: %d\n”, part1.number); printf(“part name: %s\n”, part1.name); printf(“quantity on hand: %d\n”, part1.on_hand); part1.number = 258; part1.on_hand++; scanf(“%d”, &part1.on_hand);
Structure Assignment part2 = part1; part2.number = part1.number; part2.name = part1.name; (*) part2.on_hand = part1.on_hand; struct { int a[10]; } a1, a2; a1 = a2; /*legal*/ But we can’t use the == and != operators
Structure Types struct node{ int number; char name[NAME_LEN+1]; int on_hand; } part1; Repeating the structure information will bloat the program. Either declare a “structure tag” or use typedef to define a type name • struct node{ • int number; • char name[NAME_LEN+1]; • inton_hand; • } part2;
Declaring a Structure Tag • A structure tag is a name used to identify a particular kind of structure. struct part{ int number; char name[NAME_LEN+1]; int on_hand; }; Should not be omitted
Declaring a Structure Tag struct part part1, part2; part part1, part2; // WRONG struct part part1 = {52, “Disk drive”, 10}; struct part part2; part2 = part1;
Defining a Structure Type typedef struct{ int nubmer; char name[NAME+1]; int on_hand; } Part; Part part1, part2;
Structure as Arguments and Return Values print_part(struct part p) { printf(“part number: %d\n”, part1.number); printf(“part name: %s\n”, part1.name); printf(“quantity on hand: %d\n”, part1.on_hand); } print_part(part1);
Structure as Arguments and Return Values struct part build_part(int number, const char *name, int on_hand) { struct part p; p.number = number; strcpy(p.name, name); p.on_hand = on_hand; return p; }
Nested Arrays and Structures struct person_name{ char first[FIRST_NAME_LEN+1]; char middle_initial; char last[LAST_NAME_LEN+1]; } struct student{ struct person_name name; int id, age; char sex; }student1, student2;
Nested Arrays and Structures strcpy(student1.name.first, “Fred”); display_name(student1.name);
Array of Structures struct part inventory[100]; struc dialing_code{ char *country; int code; } const struct dialing_code country_codes[] = {{“Argentina”, 54}, {“Brazil”, 55}, …, {“U.S”, 1}, {“Vietnam”, 84} };
Unions union{ int i; double d; } u;
Enumerations • enum suit {CLUBS, DIAMONS, HEARTS, SPADES}; • enum suit s1, s2; • typedef enum {CLUBS, DIAMONS, HEARTS, SPADES} Suit; • Suit s1, s2;
Enumerations as Integers • enum suit {CLUBS=1, DIAMONDS=2, HEARTS=3, SPADES=4}; • enum dept {RESEARCH=20, PRODUCTION=10, SALES=25}; • enum EGA_colors {BLACK, LT_GRAY=7, DK_GRAY, WHITE=15}; int i = DIAMONDS; Suit s = 0; s++; i = s + 2;