Once a pointer to a structure has been declared and been
set to the address of an instance of the structure, the
-> operator can be used to access the members.
Program
/* File: structptrs.c
This program shows how to use the pointer structure member
operator.
*/
#include
#include
typedef struct dummy_tag * studentRecPtr ;
typedef struct dummy_tag
{
char name [25] ;
char major[25] ;
double gpa ;
studentRecPtr teammate1 ;
studentRecPtr teammate2 ;
} studentRecord ;
main()
{
studentRecPtr ptr;
studentRecord student1;
int i, size ;
/* make up some students */
strcpy(student1.name, "Cal Ripken, Jr.") ;
strcpy(student1.major,"Business Management") ;
student1.gpa = 2.0 ;
student1.teammate1 = student1.teammate2 = NULL ;
/* Set ptr to point to Cal Ripken */
ptr = &student1;
/* Print out Cal's stats */
printf("The Ironman himself:\n") ;
printf("%s\n", ptr -> name);
printf("%s\n", ptr -> major);
printf("%1.4f\n", ptr -> gpa);
}
Output
retriever[102] a.out
The Ironman himself:
Cal Ripken, Jr.
Business Management
2.0000
retriever[103]