Monday, 6 April 2020

Self Referential Structures in C with example

A Self Referencial Structure is a structure which have one or more pointer variables of that structure type as their member(s).

Consider following example:
struct student{
   int rollno;
   float marks;
};

Above structure declaration is standard declaration we studied earlier. But if we add one or more pointer variables of structure student type as the member(s) of student structure, then it will become self referencial structure. See the example below:

struct student{
     int rollno;
     float marks;
     struct student *next;

}

In the above example ‘next’ is a pointer to a structure of type ‘student. Hence, the structure ‘student is a self-referential structure with ‘next as the referencing pointer.




A self referential structure is used to create data structures like linked lists, stacks, queues etc.


You may also like:
 Structure in C
 Typedef in C with examples
 Array of Structures in C with Example

Please comment and share if you find it useful.

  

No comments:

Post a Comment