EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Nested Structure in C

Home » Software Development » Software Development Tutorials » C Programming Tutorial » Nested Structure in C

Nested Structure in C

Introduction to Nested Structure in C

Any programming language has its own way of defining and describing structures. So Nested structures as its name suggest in C is kind of defining one structure inside another structure. Any member variables can be defined inside a structure and in turn, that structure can further be moved into another structure. The variables inside a structure can be anything like normal or pointer or anything and can be placed anywhere within the structure.

Nested Structure can be accessed in two ways:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

  1. Structure inside a structure in C using the pointer variable.
  2. Structure inside a structure in C using a normal variable.

Syntax:

Following is the syntax for creating a nested structure:

structure tagname_1
{
var_1;
var_2;
var_3;
.
.
.
.
var n;
structure tagname_2
{
var_1;
var_2;
var_3;
.
.
.
var_n;
}, mem1
} mem2;

Working of Nested Structure in C

From the above syntax, we can infer the fact that mem1 structure nested inside member1 structure will contain the member or the variable to be accessed and everyone can be accessed in a nested manner by using. (dot) operator.

  • mem2.mem1.var_1: This refers to the first member of the variable of the structure tagname_1.
  • mem2.mem1.var_2: This refers to the second member of the variable of the structure tagname_2.

We will take more examples to get clarity on how the syntax satisfies the working of the nested structure.

Examples #1

struct employee
{
struct man
{
char name [20];
int age;
char dob[10];
} d;
int empid;
char desg[10];
} emp;

In the above example, man structure is defined inside an employee structure which is a nested structure. Members within the nested structure which is a man can be accessed using the below syntax or format.

Popular Course in this category
C Programming Training (3 Courses, 5 Project)3 Online Courses | 5 Hands-on Projects | 34+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,570 ratings)
Course Price

View Course

Related Courses
C++ Training (4 Courses, 5 Projects, 4 Quizzes)Java Training (40 Courses, 29 Projects, 4 Quizzes)

Like in the given example

  • employee.d .name: It tells about the name of the man inside the employee structure.
  • employee.d .age: It will tell about the age of the man as an employee.

It is important to bring into notice one thing like this structure man within employee structure cannot be reused which means it cannot be called again anywhere in the entire code because It is not self-generated.

Instead, a workaround for this can be:

We could have defined the structure outside and then could have declared the variable inside the structure wherever we want to access it throughout the code.

Examples #2

Struct man
{
char name[20];
int age;
char dob [10];
};

Also, this structure can be reused by the outer structure.

struct employee
{
struct man info;
int id;
char desg [10];
}

The advantage of using this type of structure declaration is that we can declare a variable of type struct man anywhere throughout the program.

Note: Nesting of structure within itself is never allowed.

Let’s see an example of how nesting of structure within itself is not allowed.

struct teacher
{
char name[20];
char address[100];
int age[];
struct teacher principal; // totally invalid way to create nested structure.
}

Examples of Nested Structures in C

Below are the different examples of nested structure in C:

Example #1 – Initialization of nested structures:

Initialization of nested structures is possible at the time of declaration.

Code:

struct student
{
struct person info;
int rollno;
float marks[10];
}
struct student student_1 = {
{"Anji", 26, 1995},
103,
92
};

Example

Code:

#include <stdio.h>
struct person
{
char name[20];
int age;
char dob[10];
};
struct student
{
struct person info;
int roll_no;
float marks;
};
int main ()
{
struct student p1;
printf("Details of student: \n\n");
printf("Enter name: ");
scanf("%s", p1.info.name);
printf("Enter age: ");
scanf("%d", &p1.info.age);
printf("Enter dob: ");
scanf ("%s", p1.info.dob);
printf("Enter roll no: ");
scanf("%d", &p1.roll_no);
printf("Enter marks: ");
scanf ("%f", &p1.marks);
printf("\n.......................\n\n");
printf("Name: %s\n", p1.info.name);
printf("Age: %d\n", p1.info.age);
printf("DOB: %s\n", p1.info.dob);
printf("Roll no: %d\n", p1.roll_no);
printf("Marks: %.2f\n", p1.marks);
return 0;
}

Output:

Nested Structure in C Output-1

Example #2 – Accessing of members inside nested structure using Pointers:

Code:

#include <stdio.h>
#include <string.h>
struct student_college_info
{
int college_id;
char college_name[50];
};
struct student_info
{
int id;
char name[20];
float percentage;
struct student_college_info clg_data;
} stu_data, *stu_data_ptr;
int main()
{
struct student_info stu_data = {2, "Anu", 92.5, 81145,
"SRM University"};
stu_data_ptr = &stu_data;
printf(" Id is: %d \n", stu_data_ptr->id);
printf(" Name is: %s \n", stu_data_ptr->name);
printf(" Percentage is: %f \n\n",
stu_data_ptr->percentage);
printf(" College Id is: %d \n",
stu_data_ptr->clg_data.college_id);
printf(" College Name is: %s \n",
stu_data_ptr->clg_data.college_name);
return 0;
}

Output:

Nested Structure in C Output-2

Example #3 – Passing structure member as arguments to function:

Code:

struct teacher
{
char name [20];
int id;
int marks;
};
void print_struct (char name [], int id, int marks);
int main ()
{
struct teacher tea = {"nidhi", 5, 52};
print_struct (tea.name, tea.id, tea.marks);
return 0;
}
void print_struct (char name [], int id, int marks)
{
printf ("Name: %s\n", name);
printf ("id: %d\n", id);
printf ("Marks: %d\n", marks);
printf("\n");
}

Output:

Output-3

Example #4 – Structure inside structure using a normal variable.

Code:

#include <stdio.h>
#include <string.h>
struct student_college_detail
{
nt college_id;
char college_name[50];
};
struct student_detail
{
int id;
char name[20];
float percentage;
struct student_college_detail clg_data;
} stu_data;
int main()
{
struct student_detail stu_data = {8, "Moam", 50.5, 562345,
"CSS University"};
printf(" Id is: %d \n", stu_data.id);
printf(" Name is: %s \n", stu_data.name);
printf(" Percentage is: %f \n\n", stu_data.percentage);
nbsp;
printf(" College Id is: %d \n",
stu_data.clg_data.college_id);
printf(" College Name is: %s \n",
stu_data.clg_data.college_name);
return 0;
}

Output:

Output-4

Example

Code:

#include <stdio.h>
#include <string.h>
struct student
{
int id1;
int id2;
char e;
char f;
float percentage;
};
int main ()
{
int i;
struct student recrd1 = {3, 4, 'C', 'B', 80.5};
printf ("size of structure in bytes: %d\n",
sizeof(recrd1));
printf ("\nAddress of id1        = %u", &recrd1.id1);
printf("\nAddress of id2        = %u", &recrd1.id2 );
printf("\nAddress of a          = %u", &recrd1.e );
printf("\nAddress of b          = %u", &recrd1.f );
printf("\nAddress of percentage = %u”, &recrd1.percentage);
return 0;
}

Output:

Output-5

Note: Although it is good to pass structure variables as an argument because it allows us to pass all members of structure to function but still this is not a conventional method to do so.

Conclusion

Structures in C is a very interesting way to cluster and group all user-defined member variables and functions into one entity. But still, it has some limitations like it does not allow structure variables and entire structure to contain the build-in datatypes and no use of operators. Therefore in the mere future maybe these features can be taken care of.

Recommended Articles

This is a guide to the Nested Structure in C. Here we discuss the working in Nested Structure in C along with different examples and code implementation. You may also look at the following article to learn more –

  1. Patterns in C Programming
  2. Left Shift Operator in C
  3. Expression Evaluation in C
  4. Decimal to Hexadecimal in C

C Programming Training (3 Courses, 5 Project)

3 Online Courses

5 Hands-on Projects

34+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

1 Shares
Share
Tweet
Share
Primary Sidebar
C Programming Tutorial
  • Control Statement
    • Control Statements in C
    • If Statement in C
    • If-else Statement in C
    • Else if Statement in C
    • Nested if Statement in C
    • #else in C
    • Structure Padding in C
    • Nested Structure in C
    • Continue Statement in C
    • Break Statement in C
    • Switch Statement in C
    • Goto Statement in C
  • Basic
    • Introduction to C
    • What is C
    • Career in C Programming
    • Advantages of C
    • How to Install C
    • Best C Compilers
    • Data Types in C
    • Variables in C
    • C Keywords
    • C Command
    • Command Line Arguments in C
    • C Literals
    • Constants in C
    • Unsigned Int in C
    • String in C
  • Pointers
    • Pointers in C
    • Null pointer in C
    • Function Pointer in C
    • Double Pointer in C
    • Void Pointer in C
    • Const Pointer in C
    • Dangling Pointers in C
    • Pointer Arithmetic in C
  • Operators
    • C Operators
    • Arithmetic Operators in C
    • Relational Operators in C
    • Assignment Operators in C
    • Logical Operators in C
    • Conditional Operator in C
    • Modulus Operator in C
    • Ternary Operator in C
    • Address Operator in C
    • Unary Operator in C
    • Operators Precedence in C
    • Left Shift Operator in C
  • Loops
    • Loops in C
    • For Loop in C
    • While Loop in C
    • Do While Loop in C
    • Nested Loop in C
    • Infinite Loop in C 
  • Function
    • Math Functions in C
    • Hashing Function in C
    • Recursive Function in C
    • Power Function in C
    • fputs in C
    • C puts() Function
    • fprintf() in C
    • fseek() in C
    • Stderr in C
    • ASCII Value in C
    • strcat() in C
    • Inline Function in C
    • sizeof() in C
    • Function Prototype in C
    • C ftell()
  • Array
    • Arrays in C Programming
    • 2-D Arrays in C
    • 3D Arrays in C
    • Multidimensional Array in C
    • Array Functions in C
    • Strings Array in C
  • Sorting
    • Sorting in C
    • Heap Sort in C
  • Advanced
    • Constructor in C
    • Encapsulation in C
    • C Storage Classes
    • Static Keyword in C
    • File Handling in C
    • Queue in C
    • Hexadecimal in C 
    • typedef in C
    • Memory Allocation in C
    • Linked List in C
    • Volatile in C
    • Tokens in C
    • Expression in C
    • Regular Expression in C
    • Error Handling in C
    • Types of Errors in C
    • Preprocessor in C
    • Preprocessor Directives in C
    • fscanf() in C
    • #Pragma in C
    • #ifndef in C
    • #undef in C
    • Macros in C
  • C programs
    • Patterns in C Programming
    • Star Patterns in C
    • Number Patterns in C
    • Swapping in C
    • Reverse Number in C
    • Palindrome in C Program
    • Factorial in C
    • Fibonacci Series in C
    • Square Root in C
    • Random Number Generator in C
    • Prime Numbers in C
    • Escape Sequence in C
    • Reverse String in C
    • Leap Year Program in C
    • Anagram Program in C
    • Strong Number in C
    • String Concatenation in C
    • C Programming Matrix Multiplication
    • Decimal to Octal in C
    • Expression Evaluation in C
    • Decimal to Hexadecimal in C
  • Interview question
    • C Programming Interview Questions

Related Courses

C Programming Training Course

C++ Training Course

Java Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - C Programming Training (3 Courses, 5 Project) Learn More