Programming in C: Unit IV: Structures and Union

Self-referential Structures

Syntax with Example C Programs

Self-referential structures are those structures that contain a reference to data of its same type, i.e., in addition to other data, a self-referential structure contains a pointer to a data that is of the same type as that of the structure.

SELF-REFERENTIAL STRUCTURES

Self-referential structures are those structures that contain a reference to data of its same type, i.e., in addition to other data, a self-referential structure contains a pointer to a data that is of the same type as that of the structure. For example, consider the structure node given as follows:

struct node

{

int val;

struct node *next;

};

Here the structure node will contain two types of data-an integer val and next, which is a pointer to a node. You must be wondering why we need such a structure. Actually, self-referential structure is the foundation of other data structures. We will be using them throughout this book and their purpose will be clear to you when we will be discussing linked lists, trees, and graphs.

Programming Tip: It is an error to use a structure/ union variable as a member of its own struct type structure or union type union, respectively.

Programming in C: Unit IV: Structures and Union : Tag: : Syntax with Example C Programs - Self-referential Structures