UMBC CS 201, Spring 00
UMBC CMSC 201 Spring '00 CSEE | 201 | 201 S'00 | lectures | news | help

Self-Referencing Structures

We just saw how structures can point to other structures. It's also possible for stuctures to point to structures of the same type. 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.

Note that since the type definition of this new structure hasn't been completed until after all of the members have been declared with their types, we would need to use the tag to indicate a pointer to this type of structure.

typedef struct student { char name [25] ; char major[25] ; double gpa ; struct student *teammate1 ; struct student *teammate2 ; } STUDENT ;

Some folks find pointers to structures to be confusing. One way to (possibly) make them easier is to use typedefs for pointers to structs. Doing so can eliminate some of the syntax by hiding the *.

Using our student structure as an example, we can declare the following typedef (even before struct student is defined):

typedef struct student *STUDENTPTR; This make STUDENTPTR an alias for 'struct student *', a pointer to a student struct. We can then declare variables to be pointers to student structs with STUDENTPTR ptr; /* no '*' needed, its in the typedef */

The Program

/* File: structself.c Author: D. Frey Date: 1/24/00 Section: 101 SSN: 123-45-6789 E-Mail: frey@cs.umbc.edu This program uses structures that include fields which are pointers to these structures. */ #include <stdio.h> #include <string.h> /* make an alias for the pointer */ typedef struct student *STUDENTPTR; typedef struct student { char name [25] ; char major[25] ; double gpa ; STUDENTPTR teammate1 ; STUDENTPTR teammate2 ; } STUDENT ; /* Function prototypes */ void PrintRecord (STUDENTPTR) ; main() { STUDENT student1, student2, student3; STUDENTPTR ptr; /* ** STUDENT *ptr; ** struct student *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, "Will Clark") ; strcpy(student3.major, "Business Management") ; student3.gpa = 2.1 ; student3.teammate1 = student3.teammate2 = NULL ; /* Make Ripken, Mussina and Clark 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) ; } /************************************* ** Function PrintRecord ** Print out a single record neatly ** Inputs: a pointer to the STUDENT to be printed ** Output: all STUDENT members are displayed ** there is no return value ***************************************/ void PrintRecord (STUDENTPTR pStudent) { printf("%25s, %25s, GPA: %1.4f\n", pStudent->name, pStudent->major, pStudent->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: Will Clark, Business Management, GPA: 2.1000 retriever[104]


CSEE | 201 | 201 S'00 | lectures | news | help

Wednesday, 12-Apr-2000 12:30:27 EDT