9.3 Accessing Data Fields
As you have seen in the previous sections, the individual fields of a struct variable can be accessed using dot notation. Thus, given the struct variable
any of the fields defined for the struct can be directly accessed and treated like any other variable. Here, we use the strcpy function to save a copy of the street address stored in the struct variable:
- char street[STREET_NAME_SIZE];
- strcpy(street, whiteHouse.street);
which results in
In the C programming language, the period used in the dot notation is treated as an operator. As such, it can not be used to access a data field of a struct when the struct is referenced via a pointer. Consider the pointer declaration below:
- struct Address *record;
- record = &whiteHouse;
If we attempt to reference the number field using the dot notation
- record.number = 1600;
a syntax error will occur. Remember, a pointer in C has to be dereferenced in order to access the variable to which the pointer refers. In this case, the pointer is referring to the whiteHouse struct variable. Instead, we must use a special operator, ->, in place of the dot to indicate that we want to "follow the pointer and access a field":
- record->number = 1600;
- strcpy(street, record->street);
Since the data fields of a struct are treated like any other variable, you can get the address of a specific field using the & operator:
- int *number = &whiteHouse.number;
Since the dot operator (.) has precedence over the address operator (&), the address returned is that of the data field and not of the struct variable itself.
The dot operator (
to access a data field of a struct via a pointer. Here, the parentheses are necessary due to the order of operator precedence. In C, the dot operator has higher precedence than the pointer dereference operator ( |
Given the record pointer defined above, how would you print the address of the White House?
- printf("%d %s\n", record->number, record->street);
- printf("%s, %s %d\n", record->state, record->zipcode);
Given the two pointer variables
- struct Card *one;
- struct Card *two;
that have already been initialized to point to Card structs (as defined in Section 9.1), which of the following will correctly copy the entire contents of the struct pointed to by one to the struct pointed to by two?
two = one;two = *one;one = *two;*two = *one;
Given the memory snapshot illustrated in the previous question with the two pointer variables, one and two, which point to Card structs, which of the following statements correctly modifies the seven of diamonds card to make it the seven of clubs?
two = one;two->suit = 'C';two.suit = 'C';two.suit = one.suit;
Consider the declarations below:
- struct Pixel {
- unsigned char red;
- unsigned char green;
- unsigned char blue;
- };
- struct Pixel pix1;
- struct Pixel pix2;
- struct Pixel *p = &pix1;
- struct Pixel *q = &pix2;
For each of the following assignment statements, indicate whether the expression is valid or invalid (contains a syntax error).
- valid|invalid
pix1 = pix2;pix1.red = p.red;pix1->red = pix2->red;p->green = 0;pix2.blue = q->blue;pix1 = *q;*p = *q;Pixel.red = pix1.red;

