Ad Code

Responsive Advertisement

Nested Structure in C ?With Example

 

Nested Structure in C


C provides us the feature of nesting one structure within another structure by using which, complex data types are created. For example, we may need to store the address of an entity employee in a structure. The attribute address may also have the subparts as street number, city, state, and pin code. Hence, to store the address of the employee, we need to store the address of the employee into a separate structure and nest the structure address into the structure employee. Consider the following program.

c
#include <stdio.h>
// Define a structure for a point in 2D space 
struct Point { 
int x; 
int y; 
}; 
// Define a structure for a rectangle using the Point structure 
struct Rectangle { 
struct Point upperLeft; 
struct Point lowerRight;
}; 
int main()
// Declare and initialize a Point 
struct Point p1 = {10, 20}; 
// Declare and initialize a Rectangle using the Point structure
struct Rectangle rect = {{5, 10}, {15, 25}}; 
// Accessing members of the Point structure 
printf("Point: (%d, %d)\n", p1.x, p1.y); 
// Accessing members of the Rectangle structure 
printf("Rectangle Upper Left: (%d, %d)\n", rect.upperLeft.x, rect.upperLeft.y); 
printf("Rectangle Lower Right: (%d, %d)\n", rect.lowerRight.x, rect.lowerRight.y); 
return 0;
 }

Output:
mathematica
Point: (10, 20)
Rectangle Upper Left: (5, 10) 
Rectangle Lower Right: (15, 25)


The structure can be nested in the following ways.
1. By separate structure
2. By Embedded structure

1) Separate structure

Here, we create two structures, but the dependent structure should be used inside the main structure as a member. Consider the following example.
c
#include <stdio.h> 
// Define a structure for a point in 2D space 
struct Point {
int x; 
int y; 
}; 
// Define a structure for a rectangle using the Point structure
struct Rectangle { 
struct Point upperLeft; 
struct Point lowerRight; }; 
int main()
// Declare and initialize a Point 
struct Point p1 = {10, 20}; 
// Declare and initialize a Rectangle using the Point structure 
struct Rectangle rect;
rect.upperLeft.x = 5;
 rect.upperLeft.y = 10;
 rect.lowerRight.x = 15;
 rect.lowerRight.y = 25
// Accessing members of the Point structure 
printf("Point: (%d, %d)\n", p1.x, p1.y); // Accessing members of the Rectangle structure 
printf("Rectangle Upper Left: (%d, %d)\n", rect.upperLeft.x, rect.upperLeft.y); 
printf("Rectangle Lower Right: (%d, %d)\n", rect.lowerRight.x, rect.lowerRight.y); 
return 0
}

As you can see, doj (date of joining) is the variable of type Date. Here doj is used as a member in Employee structure. In this way, we can use Date structure in many structures.

2) Embedded structure

The embedded structure enables us to declare the structure inside the structure. Hence, it requires less line of codes but it can not be used in multiple data structures. Consider the following example.

:

c
#include <stdio.h>
// Define a structure for a point in 2D space 
struct Point { 
int x; 
int y; 
}; 
// Define a structure for a rectangle using an embedded Point structure 
struct Rectangle { 
struct Point upperLeft; 
struct Point lowerRight; }; 
int main()
// Declare and initialize a Point 
struct Point p1 = {10, 20}; 
// Declare and initialize a Rectangle using an embedded Point structure 
struct Rectangle rect =
 {5, 10}, // upperLeft
{15, 25} // lowerRight 
 };
// Accessing members of the Point structure 
printf("Point: (%d, %d)\n", p1.x, p1.y); // Accessing members of the Rectangle structure 
printf("Rectangle Upper Left: (%d, %d)\n", rect.upperLeft.x, rect.upperLeft.y); 
printf("Rectangle Lower Right: (%d, %d)\n", rect.lowerRight.x, rect.lowerRight.y); 
return 0
}


Accessing Nested Structure


We can access the member of the nested structure by Outer_Structure.Nested_Structure.member as given below:
c
#include <stdio.h>
// Define a structure for a point in 2D space 
struct Point { 
int x; 
int y; 
}; 
// Define a structure for a rectangle using an embedded Point structure 
struct Rectangle { 
struct Point upperLeft; 
struct Point lowerRight;
}; 
int main()
// Declare and initialize a Point 
struct Point p1 = {10, 20}; 
// Declare and initialize a Rectangle using an embedded Point structure 
struct Rectangle rect =
 {5, 10}, // upperLeft
{15, 25} // lowerRight }; 
// Accessing members of the Point structure 
printf("Point: (%d, %d)\n", p1.x, p1.y); 
// Accessing members of the Rectangle structure
printf("Rectangle Upper Left: (%d, %d)\n", rect.upperLeft.x, rect.upperLeft.y); 
printf("Rectangle Lower Right: (%d, %d)\n", rect.lowerRight.x, rect.lowerRight.y); 
return 0;
 }

In this example:
To access the x and y members of the Point structure within the Rectangle, you use the dot operator twice: rect.upperLeft.x and rect.upperLeft.y.
Similarly, for the lower-right corner, you use rect.lowerRight.x and rect.lowerRight.y

Passing structure to function

Just like other variables, a structure can also be passed to a function. We may pass the structure members into the function or pass the structure variable at once. Consider the following example to pass the structure variable employee to a function display() which is used to display the details of an employee.
c
#include <stdio.h>
// Define a structure for a point in 2D space
struct Point {
int x; 
int y; 
}; 
// Function that takes a Point structure as an argument 
void printPoint(struct Point p)
printf("Point: (%d, %d)\n", p.x, p.y);
 } 
int main()
// Declare and initialize a Point 
struct Point p1 = {10, 20}; 
// Pass the Point structure to the function
printPoint(p1); 
return 0
}

Post a Comment

0 Comments