Book
Collection
Click for Table of Contents
© 2025 by Rance D. Necaise
C Primer for Python Programmers
Copyright © 2025
Rance D. Necaise

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:

  1. char street[STREET_NAME_SIZE];
  2. 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:

  1. struct Address *record;
  2. record = &whiteHouse;

If we attempt to reference the number field using the dot notation

  1. 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":

  1. record->number = 1600;
  2. 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:

  1. 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.

Special Topic
The Dot and Pointer Operators

The dot operator (.) and the pointer operator (->) are not interchangeable. The dot notation must be used when referring to a data field contained in a struct variable, such as with the whiteHouse struct variable. The pointer notation can only be used when referring to a data field of a struct that is pointed to by a pointer variable, such as with the record pointer variable. As an alternative to the pointer notation, you could use the equivalent construct

  1. (*record).number = 1600;

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 (*). Since the dot operator can only be used with a struct, we must first dereference the pointer, record, to refer the struct to which it points, after which, we can then access a data field of the struct.

Question 9.3.1

Given the record pointer defined above, how would you print the address of the White House?

  1. printf("%d %s\n", record->number, record->street);
  2. printf("%s, %s  %d\n", record->state, record->zipcode);
Question 9.3.2

Given the two pointer variables

  1. struct Card *one;
  2. 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?

Select the correct answer below by clicking on the appropriate box.
  1. two = one;
  2. two = *one;
  3. one = *two;
  4. *two = *one;
Question 9.3.3

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?

Select the correct answer below by clicking on the appropriate box.
  1. two = one;
  2. two->suit = 'C';
  3. two.suit = 'C';
  4. two.suit = one.suit;
Question 9.3.4

Consider the declarations below:

  1. struct Pixel {
  2.   unsigned char red;
  3.   unsigned char green;
  4.   unsigned char blue;
  5. };
  6.  
  7. struct Pixel pix1;
  8. struct Pixel pix2;
  9. struct Pixel *p = &pix1;
  10. struct Pixel *q = &pix2;

For each of the following assignment statements, indicate whether the expression is valid or invalid (contains a syntax error).

Select the correct answer by clicking on the appropriate button.
  1. valid|invalid
  2. pix1 = pix2;
  3. pix1.red = p.red;
  4. pix1->red = pix2->red;
  5. p->green = 0;
  6. pix2.blue = q->blue;
  7. pix1 = *q;
  8. *p = *q;
  9. Pixel.red = pix1.red;