2.3 struct pointers

Just as you make integers and pointers to integers, you can make structures and pointers to structures. Following our example, we can make a pointer to a student record as follows:

struct Student *pStudent;

To dereference individual components (called fields) of a structure, we can do one of the following:

pStudent->name[0] = '\0'; // more common
(*pStudent).name[0] = '\0'; // this works, too

The first method is more common. The -> operator (points-to) dereference a struct pointer and specifies a field. The second method works as well. First, (*pStudent) dereferences the pointer, so the outcome becomes a student record (struct Student). Then, we use .name to access a component called name in the structure.



Copyright © 2006-08-30 by Tak Auyeung