0% found this document useful (0 votes)
10 views

Nested & Self referential Structure

Uploaded by

21l121
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Nested & Self referential Structure

Uploaded by

21l121
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Recall

• Structure
• Syntax
• Declaration & Access
• typedef
• Array of Structures

6/25/2022 C.P.Shabariram 1
Outline
• Nested structure
• Separate structure
• Embedded structure

6/25/2022 C.P.Shabariram 2
Nested structures
• one structure within another structure
• To create complex data types
• There are two ways for nesting.
– By separate structure
– By Embedded structure

6/25/2022 C.P.Shabariram 3
Separate structure
• Create two structures struct Date
{
• The dependent structure int dd;
should be used inside int mm;
int yyyy;
the main structure as a };
member. struct Employee
{
int id;
char name[20];
struct Date doj;
}emp1;

6/25/2022 C.P.Shabariram 4
Embedded structure
• Create two structures struct Employee
{
• declare the structure int id;
inside the structure char name[20];
struct Date
• less line of codes {
• it can not be used in int dd;
int mm;
multiple data structures int yyyy;
}doj;
}emp1;

6/25/2022 C.P.Shabariram 5
Accessing Nested Structure
• Syntax
Outer_Structure.Nested_Structure.member
• Example
– e1.doj.dd
– e1.doj.mm
– e1.doj.yyyy

6/25/2022 C.P.Shabariram 6
Passing structure to function
#include<stdio.h> void display(struct employee);
struct address void main ()
{ {
char city[20]; struct employee e;
int pin; scanf("%s %s %d ",e.name,e.add.city,
}; &e.add.pin);
struct employee display(emp);
{ }
char name[20]; void display(struct employee emp)
struct address add; {
}; printf("%s %s
%d",e.name,e.add.city,e.add.pin);
}
6/25/2022 C.P.Shabariram 7
Structure Pointer
• points to the address of a memory block where
the Structure is being stored
• Declaration & Initialization
– Inside and outside of the main() function.
struct structure_name *ptr;
ptr = &structure_variable; // Initialization
Or
struct structure_name *ptr = &structure_variable;

6/25/2022 C.P.Shabariram 8
Access Structure member using
pointer
• There are two ways to access the member of
the structure using Structure pointer:
– Using ( * ) asterisk or indirection operator and
dot ( . ) operator.
– Using arrow ( -> ) operator or membership
operator.

6/25/2022 C.P.Shabariram 9
Access Structure member using
pointer
#include<stdio.h> s2->code=3251;
#include <string.h> strcpy((*s2).name, "C");
struct subject
printf( "subject 1: %d %s\n",
{ int code;
s1.code, s1.name);
char name[50];
}; printf( "subject 2: %d %s\n",
int main( ) (*s2).code, s2->name);
{ return 0;
struct subject s1; }
struct subject *s2;
s2=&s1;
//Use &s2->code in scanf

6/25/2022 C.P.Shabariram 10
Self Referential Structures
• Structures that have one or more pointers
which point to the same type of structure, as
their member.
• structures pointing to the same type of
structures
• Note: pointer should be initialized properly
before accessing, as by default it contains
garbage value.

6/25/2022 C.P.Shabariram 11
Example
struct node {
int data1;
char data2;
struct node* link;
};

int main()
{
struct node ob;
return 0;
}

6/25/2022 C.P.Shabariram 12
Types
• Self Referential Structure with Single Link
– only one self-pointer as their member.

• Self Referential Structure with Multiple Links


– Self referential structures with multiple links can
have more than one self-pointers.
– Many complicated data structures can be easily
constructed using these structures.

6/25/2022 C.P.Shabariram 13
Applications
• useful in creation of other complex data
structures like:
– Linked Lists
– Stacks
– Queues
– Trees
– Graphs

6/25/2022 C.P.Shabariram 14

You might also like