Summary: A structure pointer can access any member of a structure.
A pointer to a structure, or “structure pointer,” is conceptually similar to an array pointer. Just as an array pointer can point to any element in an array, a structure pointer can reference any member in a structure. The major difference is one of notation.
In case you're not yet an expert on structure notation, let's review it very briefly. First recall that each element in an array has the same type, so you refer to individual array elements with subscripts:
i_array[3]
Because members of a structure can have different types, you can't use numerical subscripts to refer to them based on their order. Instead, each structure member has a symbolic name. You refer to a member with a structure name and member name, separating the two names with the member-of operator (.):
jones.name
The notation for structure pointers follows the same pattern, with only two differences. You must
1.Replace the structure name with the name of the pointer
2.Replace the member-of operator with a two-character operator called the “pointer-member” operator (–>)
The pointer-member operator is formed by a dash and a right-angle bracket. The following name uses the pointer-member operator:
jones_ptr->name
Here jones_ptr is the name of a pointer to a structure, and name is a member of the structure that jones_ptr points to.
The EMPLOY1.C program is a revision of the EMPLOYEE.C program that demonstrates structures in Chapter 4, “Data Types.” This program illustrates how to manipulate a structure through a pointer:
/* EMPLOY1.C: Demonstrate structure pointers. */
#include <stdio.h>
struct employee
{
char name[10];
int months;
float wage;
};
void display( struct employee *e_ptr );
main()
{
struct employee jones =
{
"Jones, J",
77,
13.68
};
display( &jones );
}
void display( struct employee *e_ptr )
{
printf( "Name: %s\n", e_ptr->name );
printf( "Months of service: %d\n", e_ptr->months );
printf( "Hourly wage: %6.2f\n", e_ptr->wage );
}
Summary: Structure pointers allow functions to access structures that are local to other functions.
The EMPLOY1.C program gives the same output as the earlier version. But instead of passing the entire structure to the display function, this program passes a structure pointer. This method conserves memory, since the display function doesn't create a local copy of the structure. It also allows display to change members in the original structure, which is local to the main function.
The header of the display function shows that the function expects to receive a structure pointer:
void display( struct employee *e_ptr )
The expression in parentheses specifies what type of value the function expects. This expression is a bit complex, so let's look at each part individually. The expression *e_ptr indicates the function expects to receive a pointer, which it names e_ptr. It is preceded by
struct employee
which states what type of pointer e_ptr is. The struct keyword indicates e_ptr is a pointer to a structure, and the tag employee specifies the structure type.
The next item of interest in EMPLOY1.C is the function call that passes the structure pointer:
display( &jones );
This statement uses the address-of operator to pass the address of the jones structure to the display function. The address-of operator is not optional. Since we want the function to access the original structure—not a local copy—we must pass the structure's address.
When the display function executes, it creates a pointer variable named e_ptr and assigns to it the address passed in the function call. Now the display function can refer to any member of the structure indirectly through the pointer e_ptr. Within the display function, the statement
printf( "%s\n", e_ptr->name );
has the same effect that the statement
printf( "%s\n", jones.name );
has in the main function. Figure 9.2 illustrates the relationship between the structure pointer and structure members in EMPLOY1.C.
Just to confirm that the display function can access the original structure in EMPLOY1.C, try adding this statement to the end of the display function:
strcpy( e_ptr->name, "King, M" );
and this statement to the end of the main function:
printf( "%s\n", jones.name );
These changes cause EMPLOY1.C to print:
King, M
Acting indirectly through a structure pointer, the display function was able to change a structure defined elsewhere in the program.