UMBC CS 201, Fall 05
UMBC CMSC 201 Fall '05
CSEE | 201 | 201 F'05 | 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 makes STUDENTPTR an alias for 'struct student *', a pointer to a student struct. We can then declare variables to be pointers to student structs with /* no '*' needed, it's in the typedef */ STUDENTPTR ptr;

The Program

/* File: structself.c Author: D. Frey Date: 1/24/00 Section: 101 E-Mail: frey@cs.umbc.edu This program uses structures that include members 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) ; int main() { STUDENT student1, student2, student3; STUDENTPTR ptr; /* equivalent ways of declaring ptr ** STUDENT *ptr; ** struct student *ptr; */ /* make up some students */ strcpy(student1.name, "Rafael Palmeiro") ; strcpy(student1.major,"Business Management") ; student1.gpa = 2.0 ; student1.teammate1 = student1.teammate2 = NULL ; strcpy(student2.name, "Melvin Mora") ; strcpy(student2.major, "Business Management") ; student2.gpa = 1.9 ; student2.teammate1 = student2.teammate2 = NULL ; strcpy(student3.name, "B.J. Surhoff") ; strcpy(student3.major, "Business Management") ; student3.gpa = 2.1 ; student3.teammate1 = student3.teammate2 = NULL ; /* Make Palmeiro, Mora & Surhoff teammates */ student1.teammate1 = &student2 ; student1.teammate2 = &student3 ; student2.teammate1 = &student1 ; student2.teammate2 = &student3 ; student3.teammate1 = &student1 ; student3.teammate2 = &student2 ; /* Print out Rafael's stats */ printf("The first student:\n") ; PrintRecord(&student1) ; /* Print out the stats for Rafael's 1st teammate */ ptr = student1.teammate1 ; printf("\n%s's first teammate:\n", student1.name) ; PrintRecord(ptr) ; /* Print out the stats for Rafael's 2nd teammate */ ptr = student1.teammate2 ; printf("\n%s's second teammate:\n", student1.name) ; PrintRecord(ptr) ; return 0; } /************************************* ** 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: %6.4f\n", pStudent->name, pStudent->major, pStudent->gpa) ; }

The Sample Run

The first student: Rafael Palmeiro, Business Management, GPA: 2.0000 Rafael Palmeiro's first teammate: Melvin Mora, Business Management, GPA: 1.9000 Rafael Palmeiro's second teammate: B.J. Surhoff, Business Management, GPA: 2.1000


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

Sunday, 21-Aug-2005 09:53:56 EDT