Ad Code

6/recent/ticker-posts

Self referential structure | Data Structure

Self Referential Structure

The structures in which one or more pointers points to the structure of same type.

struct self {
    int s;
    struct self *ptr;
};

A structure cannot contain a member of its own type the reason behind this is if this is allowed then it becomes very difficult for compiler to know size of such struct. At the end it throws compiler error.
Although a pointer of same type can be a member because pointers to all types are of same size and compiler able to calculate size of struct.

Example is given below,




We will see the main use of self referential structure in Linked list

Post a Comment

0 Comments

Ad Code