Programming with C

⌘K
  1. Home
  2. Docs
  3. Programming with C
  4. Structures
  5. Self-referential Structures

Self-referential Structures

A self-referential structure contains a pointer member that points to a structure of the same structure type. Self-referential structure is used to create data type like linked list, trees etc.

struct student
{
char name[20];
struct node *link;
}

Example: Displaying student name using self referential structure

#include<stdio.h>
#include<string.h>
struct student{
	int rollno;
	char name[20];
	struct student *next;
};
int main()
{
	struct student s1;
	struct student s2 = {101, "UVW", NULL};
	s1.rollno = 202;
	strcpy(s1.name, "ABC");
	s1.next = &s2; // s2 node is linked to s1 node
	printf("%s\n",s1.name); // prints ABC
	printf("%s",s1.next->name); // prints UVW
	return 0;
}

Output

enter sno, sname, marks:101
abc
90
details of the student areNumber = 101
name = abc
marks =90.000000

Views: 0

How can we help?

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments