Categories: C

Nested Structure in C

C allows us to nest one structure within another structure, which allows us to design complicated data types. This type of structure is known as nested structure.

For example, we may need to store the result of an entity student in a structure. The attribute result may also have the subparts like subject name, marks, and status. Hence, to store the result of the student, we need to store the result of the student into a separate structure and nest the structure result into the structure student.

Example of nested structure:
#include<stdio.h>  

struct result 
{  
    char sub_name[20];  
    float marks;
   
}; 
struct student   
{  
    char name[20];  
    int roll;  
    char class[14];  
    struct result res;
};  
void main ()  
{  
    struct student stu;  
    printf("Enter student information?\n");  
    scanf("%s %s %d %f",stu.name,stu.res.sub_name, &stu.roll, &stu.res.marks);  
    printf("Printing the student information....\n");  
    printf("Name: %s Subject: %s Roll: %d Marks: %f",stu.name,stu.res.sub_name, stu.roll, stu.res.marks);  
}
Output:
Enter student information?
coderzpy maths 21 99
Printing the student information....
Name: coderzpy Subject: maths Roll: 21 Marks: 99.000000
The structure can be nested as shown below.
  • Independent structure
  • Embedded structure
Independent structure:

Here, we establish two structures, however, the dependent structure should be used as a member of the primary structure. For instance,

struct Date  
{  
   int dd;  
   int mm;  
   int yyyy;   
};  
struct Employee  
{     
   int id;  
   char name[20];  
   struct Date doj;  
}emp1;  
Embedded structure:

We can declare the structure within the structure using the embedded structure. As a result, it takes fewer lines of code but cannot be utilized in numerous data structures. For instance,

struct Employee  
{     
   int id;  
   char name[20];  
   struct Date  
    {  
      int dd;  
      int mm;  
      int yyyy;   
    }doj;  
}emp1;  

Note: also read about the Structure in C& Array of Structure

Follow Me

If you like my post please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

3 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

2 years ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

2 years ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

2 years ago