Nested Structures

The structure that contains another structure as its members is called a nested structure or a structure within a structure is called nested structure. The structure should be declared separately and then be grouped into high level structure.

 

Passing Structures through pointers
Pointer to a structure is a variable that holds the address of a structure. The syntax to declare pointer to a structure can be given as:

strcut struct_name *ptr;

 

To assign address of stud to the pointer using address operator(&) we would write
ptr_stud=&stud;

 

To access the members of the structure (->) operator is used.
for example
Ptr_stud->name=Raj;

 

SELF REFERENTIAL STRUCTURE
Self –referential structures are those structures that contain a reference to data of its same type as that of structure.

Example
struct node
{
int val;
struct node*next;
};

 

Pointers to Structures
You can define pointers to structures in very similar way as you define pointer to any other variable as follows:

struct books *struct_pointer;

 

Now, you can store the address of a structure variable in the above defined pointer variable. To find the address of a structure variable, place the & operator before the structure’s name as follows:

struct_pointer = &book1;

 

To access the members of a structure using a pointer to that structure, you must use the -> operator as follows:

struct_pointer->title;

 

Try Now – Programming In C MCQs