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

Pointers to Structures

Continuing with our discussion of structures, this example will expand the student record type to include two new fields that point to other student records. In general, this is known as a self-referencing structure, which is a structure that contains pointers to others of its own type.

Note that since the type definition of this new structure hasn't been completed until after all of the members have been shown with their types, we would need to use the tag to indicate a pointer to this type of structure. Since the structure is to be a self-referencing structure, meaning you'll be working with pointers to that type of struct, you might want to typedef a pointer to that struct. Since there will be a pointer to that type of struct as one of the members of the struct, it is often more convenient to typedef the pointer first, before typedefing the struct type itself.

Suppose that student2 is a student record and ptr is a pointer to student records.

Then after the assignment,

ptr = &student2 the name field of student2 can be accessed as either (*ptr).name or ptr -> name

Note that in the first case the parentheses are necessary because the field operator . has higher precedence than the dereference operator *.

The Program

/* File: struct6.c This program uses structures that include fields which are pointers to structures of this type. These are known as self-referencing structures. */ #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 ; /* Function prototypes */ void PrintRecord (studentRecord) ; main() { studentRecord student1, student2, student3, *ptr; 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 ; strcpy(student2.name, "Mike Mussina") ; strcpy(student2.major, "Business Management") ; student2.gpa = 1.9 ; student2.teammate1 = student2.teammate2 = NULL ; strcpy(student3.name, "Lenny Webster") ; strcpy(student3.major, "Business Management") ; student3.gpa = 2.1 ; student3.teammate1 = student3.teammate2 = NULL ; /* Make Ripken, Mussina and Webster teammates */ student1.teammate1 = &student2 ; student1.teammate2 = &student3 ; student2.teammate1 = &student1 ; student2.teammate2 = &student3 ; student3.teammate1 = &student1 ; student3.teammate2 = &student2 ; /* Print out Cal's stats */ printf("The Ironman himself:\n") ; PrintRecord(student1) ; /* Print out the stats for Cal's 1st teammate */ ptr = student1.teammate1 ; printf("\n%s's first teammate:\n", student1.name) ; PrintRecord(*ptr) ; /* Print out the stats for Cal's 2nd teammate */ ptr = student1.teammate2 ; printf("\n%s's second teammate:\n", student1.name) ; PrintRecord(*ptr) ; } /* Print out a single record neatly */ void PrintRecord (studentRecord rec) { printf("%25s, %25s, GPA: %1.4f\n", rec.name, rec.major, rec.gpa) ; }

The Sample Run

retriever[103] a.out The Ironman himself: Cal Ripken, Jr., Business Management, GPA: 2.0000 Cal Ripken, Jr.'s first teammate: Mike Mussina, Business Management, GPA: 1.9000 Cal Ripken, Jr.'s second teammate: Lenny Webster, Business Management, GPA: 2.1000 retriever[104]


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

Sunday, 08-Nov-1998 16:30:16 EST