UMBC CS 201, Fall 06
UMBC CMSC 201
Fall '06

CSEE | 201 | 201 F'06 | 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 Modified: 4/23/06 By: S. Evans 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, "Jeff Conine") ; 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, "Daniel Cabrera") ; strcpy(student3.major, "Business Management") ; student3.gpa = 2.1 ; student3.teammate1 = student3.teammate2 = NULL ; /* Make Conine, Mora & Cabrera teammates */ student1.teammate1 = &student2 ; student1.teammate2 = &student3 ; student2.teammate1 = &student1 ; student2.teammate2 = &student3 ; student3.teammate1 = &student1 ; student3.teammate2 = &student2 ; /* Print out Jeff's stats */ printf("The first student:\n") ; PrintRecord(&student1) ; /* Print out the stats for Jeff's 1st teammate */ ptr = student1.teammate1 ; printf("\n%s's first teammate:\n", student1.name) ; PrintRecord(ptr) ; /* Print out the stats for Jeff'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: Jeff Conine, Business Management, GPA: 2.0000 Jeff Conine's first teammate: Melvin Mora, Business Management, GPA: 1.9000 Jeff Conine's second teammate: Daniel Cabrera, Business Management, GPA: 2.1000


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

Tuesday, 22-Aug-2006 07:14:17 EDT