3/20/14 ¡ Structure • Structures group multiple (heterogeneous) Structures, Unions, and variables Enumerations o The elements of a structure (its members) aren ’ t required to have the same type. o The members of a structure have names; to select a particular member, we specify its name, not its Based on slides from K. N. King and Dianna Xu position. • In some languages, structures are called records , Bryn Mawr College and members are known as fields . CS246 Programming Paradigm Structure Operations Structure Type (Structure Tag) • Structure type declaration • Suppose that a program needs to declare several structure variables with identical members. • Structure variable declaration • A structure tag is a name used to identify a • Member assignment/reference particular kind of structure. • The declaration of a structure tag named part : • Structure initialization struct part { Structure tag • Structure assignment int number; char name[NAME_LEN+1]; int on_hand; }; • Note that a semicolon must follow the right brace. Structure Variables Declaring a Structure Tag • The declaration of a structure tag can be combined • The part tag can be used to declare variables: with the declaration of structure variables : struct part part1, part2; struct part { • We cannot drop the word struct : int number; char name[NAME_LEN+1]; part part1, part2; /*** WRONG ***/ int on_hand; part isn ’ t a type name; without the word } part1, part2; • All structures declared to have type struct part are struct , it is meaningless. compatible with one another: • Since structure tags aren ’ t recognized unless struct part part1 = {528, "Disk drive", 10}; preceded by the word struct , they don ’ t conflict struct part part2; with other names used in a program. part2 = part1; /* legal; both parts have the same type */ 1 ¡
3/20/14 ¡ Structure Representation Type Definition • Abstract representations of a structure: • The #define directive can be used to create a “ Boolean type ” macro: #define BOOL int • A better way to define a synonym for existing (complicated) types is to use type definition: • Appearance of part1 • Assumptions: typedef int Bool; typedef int* Intptr; o part1 is located at address 2000. • Array and pointer types cannot be defined as o Integers occupy four bytes. macros. o NAME_LEN has the value 25. o There are no gaps between the members. • typedef names are subject to the same scope rules as variables. typedef and Structures Structure Variable Declaration • Instead of struct part { typedef struct part { struct part part1; int number; int number; use char name[NAME_LEN+1]; char name[NAME_LEN+1]; int on_hand; int on_hand; typedef struct part Part; } part1, part2; } Part; then int main() { int main() { Part part1; struct part part3; Part part1, part2, part3; /* skipped */ /* skipped */ • Part is a new user-defined type and can be used } } in the same way as the built-in types. • typedef ed type names by convention have the • When it comes time to name a structure, we can usually choose either to declare a structure tag or to use first letter in uppercase. typedef . Scope of Structure Variables Initializing Structure Variables • Each structure represents • A structure declaration may include an initializer: a new scope. struct part{ struct part{ int number; int number; • Any names declared in char name[NAME_LEN+1]; char name[NAME_LEN+1]; int on_hand; that scope won ’ t conflict int on_hand; } part1, part2; } part1 = {528, "Disk drive", 10}, with other names in a part2 = {914, "Printer cable", 5}; struct employee{ program. • Appearance of part1 after initialization: char name[NAME_LEN+1]; • In C terminology, each int number; char sex; structure has a separate } employee1, employee2; name space for its members. 2 ¡
3/20/14 ¡ Initializing Structure Variables Member Reference/Assignment • Structure initializers follow rules similar to those • To access a member within a structure, we write for array initializers. o structVar . memberName • An initializer can have fewer members than the printf("Part number: %d\n", part1.number); structure it ’ s initializing. printf("Part name: %s\n", part1.name); printf("Quantity on hand: %d\n", part1.on_hand); • Any “ leftover ” members are given 0 as their initial value. • The members of a structure are lvalues. o structVar . memberName = exp; • Like array initializations, this only works at the time of declaration. part1.number = 258; /* changes part1's part number */ • Afterwards you must assign/initialize each member part1.on_hand++; one by one. /* increments part1's quantity on hand */ . Operator Structure Assignment • The period used to access a structure member is • The other major structure operation is assignment: actually a C operator. part2 = part1; • The effect of this statement is to copy • It takes precedence over nearly all other operators. part1.number into part2.number , • Example: part1.name into part2.name , and so on. • Each member ’ s value will be copied scanf("%d", &part1.on_hand); • Arrays can ’ t be copied using the = operator, but an The . operator takes precedence over the & array embedded within a structure is copied when the operator, so & computes the address of enclosing structure is copied. part1.on_hand . struct { int a[10]; } a1, a2; a1 = a2; /* legal, since a1 and a2 are structures */ Structure Assignment Structures as Arguments • The = operator can be used only with structures of • A function with a structure argument: compatible types. void print_part(struct part p) o Two structures declared at the same time (as part1 { and part2 were) are compatible. printf("Part number: %d\n", p.number); printf("Part name: %s\n", p.name); o Structures declared using the same “ structure tag ” printf("Quantity on hand: %d\n", p.on_hand); or the same type name are also compatible. } • Other than assignment, C provides no operations • A call of print_part : on entire structures. print_part(part1); • In particular, the == and != operators can ’ t be used with structures. 3 ¡
3/20/14 ¡ Pointer to Structure Structures as Return Values • Passing a structure to a function and returning a • A function that returns a part structure: structure from a function both require making a struct part build_part(int number, copy of all members in the structure. const char *name, int on_hand) • To modify the original value, pass the pointer to a { struct part p; structure p.number = number; void updateNumOnHand(Part *b) { (*b).on_hand += 10; strcpy(p.name, name); } p.on_hand = on_hand; return p; int main() { } Part a = initialization ; updateNumOnHand (&a); • A call of build_part : return 0; } part1 = build_part(528, "Disk drive", 10); Nested Arrays and Structures Pointer to Structure • To deal with pointers to structure, the shorthand • Structures and arrays can be combined without form is more commonly used. restriction. • Pattern • Arrays may have structures as their elements, and structures may contain arrays and structures as o StructPtrVar à member_of_structure ; members. void updateNumOnHand(Part *b) { b->on_hand += 10; /* same as (*b).on_hand */ } int main() { Part a = initialization ; updateNumOnHand (&a); return 0; } Nested Structures Nested Structures • Nesting one structure inside another is often useful. • Copying the information from a person_name struct person_name { structure to the name member of a student structure char first[FIRST_NAME_LEN+1]; would take one assignment instead of three: char middle_initial; struct person_name new_name; char last[LAST_NAME_LEN+1]; … }; struct student { student1.name = new_name; struct person_name name; int id, age; char sex; } student1, student2; • Accessing student1 ’ s first name: strcpy(student1.name.first, "Fred"); 4 ¡
Recommend
More recommend