UMBC CS 201, Fall 98
UMBC CMSC 201 & 201H Fall '98 CSEE | 201 | 201 F'98 | lectures | news | help

Using the -> operator

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 <stdio.h> #include <string.h> 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]
Sunday, 08-Nov-1998 15:56:17 EST


CSEE | 201 | 201 F'98 | lectures | news | help