C Programming • Structure and Union
C Programming / Nested structures

Nested structures

Notes 4 Structure and Union

A nested structure is a structure that contains another structure as one of its members. It is useful when a record contains another related record.

Notes

A nested structure is a structure that contains another structure as one of its members. It is useful when a record contains another related record.

💡 Example

A student record may contain personal information and an address. The address itself can be represented using another structure.

Why Use Nested Structures?

Nested structures help organize complex information by dividing it into smaller related structures. This makes a large record easier to understand and manage.

💡 Example

A Student structure can contain an Address structure for storing city and PIN code.

Simple Structure Inside Another Structure

struct Address { char city[30]; int pinCode; }; struct Student { int rollNo; struct Address address; };
💡 Example

Here, address is a member of Student, and its type is struct Address.

Accessing Nested Structure Members

A member inside the nested structure is accessed by using the dot operator twice: first for the outer structure member and then for the inner structure member.

💡 Example
student.address.pinCode student.address.city

Here, student is the outer structure variable, address is its nested structure member, and pinCode and city are members of the nested structure.

Real-World Example

A company employee record may contain employee details and a separate address record. The address can be stored as a nested structure inside the employee structure.

🌍 Example

Employee → Name, ID, Address → City, PIN Code.

Practical Example

Problem Statement

Write a C program using nested structures to store a student's roll number, city and PIN code, and display all the information.

Learning Outcomes

  • Understand the concept of nested structures.
  • Define one structure as a member of another structure.
  • Access members of a nested structure using the dot operator.

Hint

First create an Address structure. Then use struct Address as a member inside the Student structure.

Theory

In a nested structure, one structure is included as a member of another structure. The inner structure members are accessed through the outer structure variable using the dot operator.

Program

#include <stdio.h> struct Address { char city[30]; int pinCode; }; struct Student { int rollNo; struct Address address; }; int main() { struct Student s1; printf("Enter roll number: "); scanf("%d", &s1.rollNo); printf("Enter city: "); scanf("%29s", s1.address.city); printf("Enter PIN code: "); scanf("%d", &s1.address.pinCode); // display student and nested address information printf("Roll Number = %d ", s1.rollNo); printf("City = %s ", s1.address.city); printf("PIN Code = %d ", s1.address.pinCode); return 0; }

Expected Output

Enter roll number: 101

Enter city: Jaipur

Enter PIN code: 302001

Roll Number = 101

City = Jaipur

PIN Code = 302001

Note

The expression s1.address.pinCode first accesses the address member of s1 and then accesses the pinCode member inside that nested structure.

Important Point

Nested structures are useful when a single record contains smaller records that have their own related data.

📌 Example

Student → Address → City, PIN Code

Quick Revision

Concept Remember
Nested Structure A structure used as a member of another structure.
Outer Structure The structure containing another structure.
Inner Structure The structure used as a member inside the outer structure.
Member Access Use the dot operator through the outer structure member.
Example student.address.city

Important Exam Questions

Short Answer Questions

  1. What is a nested structure?
  2. Why are nested structures used?
  3. How is one structure included inside another structure?
  4. How do you access a member of a nested structure?
  5. Write an example of a nested structure.

Long Answer Questions

  1. Explain nested structures in C with syntax and suitable example.
  2. Write a C program using a nested structure to store student and address information.
  3. Explain how members of a nested structure are accessed.
🎥 Recommended Learning

Watch a beginner-friendly explanation of nested structures in C programming.

▶ Watch: Nested Structures in C — Hindi

📝 Handwritten Notes

A short handwritten-style revision sheet for nested structures will be provided here.

🧠 Mind Map

Use the mind map for quick revision of outer structure, inner structure and member access.