UMBC CS 201, Fall 99
UMBC CMSC 201 Fall '99 CSEE | 201 | 201 F'99 | 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 ** Author: R. Chang ** Date: 6/6/96 ** ** This program shows how to use the pointer structure ** member operator. **********************************************/ #include <stdio.h> #include <string.h> #define SIZE 25 /* create structure for student records */ typedef struct dummy_tag { char name [SIZE] ; char major[SIZE] ; double gpa ; } studentRecord ; /* studentRecPtr is an alias for studentRecord* */ typedef studentRecord * studentRecPtr ; main() { studentRecord student1; studentRecPtr ptr = &student1; /* make up some information */ strcpy(student1.name, "Cal Ripken, Jr.") ; strcpy(ptr -> major,"Business Management") ; ptr -> gpa = 2.0 ; 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]
Monday, 30-Aug-1999 10:28:47 EDT


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