C o teststruct1 teststruct2c int main typedef struct

Info icon This preview shows pages 20–29. Sign up to view the full content.

View Full Document Right Arrow Icon
$ gcc –ansi –pedantic test_struct1.c –o test_struct1 $ test_struct2.c: int main() { typedef struct { float tax_rate; float gross_salary; } Tax_info; struct Employee { char *firstname; char *last_name; int id_number; Tax_info tax; } employees[50], *emp_ptr; emp_ptr = employees; emp_ptr->tax->gross_salary = 1200.00; return(0); } $ gcc –ansi –pedantic test_struct2.c –o test_struct2 test_struct2.c: In function `main’: test_struct2.c:24: error: invalid type argument of `->’ (have `Tax_info’) $ In this instance, tax isn’t a pointer, it’s a member of a structure.
Image of page 20

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
` Is it legal for a structure to contain a member that is the same type as the structure? ` Suppose we declare a structure such as the following: struct Self_Ref1 { int a; struct Self_Ref1 b; int c; };
Image of page 21
` This type of self-reference is not legal, because the member b is another complete structure, which will contain its own member b. This second member b is yet another complete structure, and contains its own member b, and so forth. ` The problem with this declaration is that it is INFINITELY RECURSIVE, so the compiler would have to attempt to allocate an infinite amount of storage. ` Let’s compare this with the self-referential declaration on the following slide.
Image of page 22

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
` Following is another self-referential structure. Can you distinguish it from the previous one? struct Self_Ref2 { int a; struct Self_Ref2 *b; int c; }; ` Is this kind of self-referential structure legal?
Image of page 23
` The kind of self-referential structure on the preceding slide is legal, and is used to build linked data structures, such as linked lists, and trees. ` The difference from the previous recursive structure which does not terminate is that in this case, the structure does not contain a member that is another structure of the same type, but rather, a pointer to a structure of the same type. ` The compiler of course can allocate space for a pointer to a structure, which requires a finite amount of space. ` We will see how to build a linked list with such a self-referential structure soon.
Image of page 24

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
` Structures can be initialized in much the same way as arrays. For example: struct Initialization_example { int a; short b[10]; Simple c; /* Defined with typedef above */ } y = { 10, {1, 2, 3, 4, 5}, /* initialization of array member */ {25, 1.9} /* initialization of struct member */ }; ` The initial values for the members must be in the order given in the member list, and they are separated by commas. ` Missing values (at the end of the list of values) cause the remaining members to get default initialization.
Image of page 25
` Let’s modify the previous example slightly, by declaring an additional structure variable x, without initialization: struct Init_example { int a; short b[10]; Simple c; } x, y = { 10, {1, 2, 3, 4, 5}, {25, 1.9} }; ` Now, because x and y have been declared to be of the same type, we can assign y to x: x = y; ` This will assign the value of each member of y to the corresponding member of x. ` Of course, we can also initialize, or assign to, the members of a structure one by one, with individual assignment statements, as shown on the following slide.
Image of page 26

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
` Assignment to individual members, given the following declarations: struct Init_example { int a; short b[10]; Simple c; } x, y = { 10, {1, 2, 3, 4, 5}, {25, 1.9} }; x.a = 10; x.b = {1,2,3,4,5}; x.c = {25, 1.9};
Image of page 27