Demonstrate the use of self-reference structure

 

Example : Write a program to demonstrate the use of self-reference structure.

#include<stdio.h>

#include<conio.h>

struct node // here node is name of structure

{

int x; // member

char y;

struct node *p; // Here p is pointer type member

};

 

int main()

{

struct node obj1;       // here obj1 is variable structure

// Initialization value to x and y member using first variable

obj1.p = NULL;

obj1.x = 10;

obj1.y = 20;

struct node obj2; // here obj2 is variable

 

// Initialization value to x and y member using second variable

obj2.p = NULL;

obj2.x = 30;

obj2.y = 40;

// Linking obj1 and obj2 variables

obj1.p = &obj2; //Initialization obj2 to obj1

// Accessing data members of ob2 using ob1

printf("Value of x is using object first:%d", obj1.p->x);

printf("\n Value of y is using object first:%d", obj1.p->y);

getch();

}

Output : 




Comments

Popular posts from this blog

Write a program in Program Notebook......