- C Programming Tutorial
- C - Home
- Basics of C
- C - Introduction
- C - Features
- C - Basics
- C - History
- C - Structure of C Program
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Type Casting
- C - Booleans
- Constants and Literals in C
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- Decision Making in C
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- Loops in C
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- Functions in C
- C - Functions
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- C Structures and Unions
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- File Handling in C
- C - Input & Output
- C - File I/O (File Handling)
- C Preprocessors
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- C - Macros
- C - Header Files
- Memory Management in C
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - String Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
C Programming - C Nested Structures
![]() Share with a Friend |
C Programming - C Nested Structures
C Nested Structures
In C, nested structures refer to a structure that contains another structure as a member. This allows you to represent more complex data types in an organized manner. Nested structures are useful when modeling real-world entities that contain other entities.
Syntax of Nested Structure
C
struct outer_structure {
data_type1 member1;
data_type2 member2;
struct inner_structure member3; // Nested structure
};
- inner_structure: A structure defined inside another structure.
- member3: A member of the outer structure that is itself a structure.
Example: Nested Structures
C
#include <stdio.h>
// Define an inner structure
struct Date {
int day;
int month;
int year;
};
// Define an outer structure
struct Employee {
int id;
char name[50];
struct Date birthdate; // Nested structure
};
int main() {
// Declare and initialize a structure variable
struct Employee emp = {1, "John Doe", {15, 8, 1990}};
// Access members of the outer and nested structures
printf("Employee ID: %d\n", emp.id);
printf("Employee Name: %s\n", emp.name);
printf("Employee Birthdate: %d/%d/%d\n", emp.birthdate.day, emp.birthdate.month, emp.birthdate.year);
return 0;
}
Output:
Employee ID: 1
Employee Name: John Doe
Employee Birthdate: 15/8/1990
Accessing Nested Structure Members
To access the members of a nested structure, use the dot operator twice. First, access the outer structure member, then access the member of the nested structure.
C
struct outer_structure {
struct inner_structure inner_member;
};
C
struct outer_structure outer;
outer.inner_member.inner_field;
Example: Multiple Levels of Nesting
A structure can have another structure, which in turn can have another structure. This creates a hierarchy of nested structures.
C
#include <stdio.h>
// Define an inner structure
struct Date {
int day;
int month;
int year;
};
// Define another structure
struct Address {
char street[100];
char city[50];
struct Date move_in_date; // Nested structure
};
// Define the outer structure
struct Person {
char name[50];
struct Address address; // Nested structure
};
int main() {
// Initialize the nested structure
struct Person person = {"Alice", {"1234 Elm St", "Wonderland", {1, 1, 2022}}};
// Access nested structure members
printf("Name: %s\n", person.name);
printf("Address: %s, %s\n", person.address.street, person.address.city);
printf("Move-in Date: %d/%d/%d\n", person.address.move_in_date.day, person.address.move_in_date.month, person.address.move_in_date.year);
return 0;
}
Output:
Name: Alice
Address: 1234 Elm St, Wonderland
Move-in Date: 1/1/2022
Dynamic Memory Allocation for Nested Structures
When dynamically allocating memory for nested structures, you can use pointers to structures and use malloc() or calloc() to allocate memory.
C
#include <stdio.h>
#include <stdlib.h>
// Define a structure
struct Date {
int day;
int month;
int year;
};
struct Employee {
int id;
char name[50];
struct Date birthdate; // Nested structure
};
int main() {
// Dynamically allocate memory for Employee
struct Employee *emp = (struct Employee*) malloc(sizeof(struct Employee));
if (emp != NULL) {
emp->id = 1;
strcpy(emp->name, "John Doe");
emp->birthdate.day = 15;
emp->birthdate.month = 8;
emp->birthdate.year = 1990;
// Access the nested structure members
printf("Employee ID: %d\n", emp->id);
printf("Employee Name: %s\n", emp->name);
printf("Employee Birthdate: %d/%d/%d\n", emp->birthdate.day, emp->birthdate.month, emp->birthdate.year);
// Free allocated memory
free(emp);
}
return 0;
}
Advantages of Nested Structures
- Organized Data Representation:
- Nested structures allow for a logical grouping of related data.
- Modeling Real-World Entities:
- They help model complex real-world entities with hierarchical relationships.
- Better Code Readability:
- Code becomes more readable and maintainable when related data is grouped in a structured way.
Disadvantages of Nested Structures
- Increased Complexity:
- More levels of nesting can make the structure difficult to manage and access.
- Memory Usage:
- Nested structures can increase memory usage due to their multiple levels.
Conclusion
Nested structures in C are useful for representing complex data with hierarchical relationships. They allow for logical grouping and improve code organization. By accessing nested members using the dot operator, we can efficiently model real-world entities such as employees, students, or products with multiple attributes.
