What is a Nested Structure?
A nested structure is a structure that contains another structure as one of its members. This feature is particularly useful for representing entities that have a hierarchical or composite relationship.​
Syntax of Nested Structure
struct Inner { int innerValue; }; struct Outer { char label[20]; struct Inner nested; // Nested structure };
Types of Nested Structures in C
There are two primary ways to define nested structures in C:
1. Embedded Structure
In this approach, the nested structure is defined within the parent structure.​
struct Outer { int id; struct { char city[50]; int zip; } address; };
2. Separate Structure
Here, the nested structure is defined separately and then used as a member in the parent structure.​
struct Address { char city[50]; int zip; }; struct Outer { int id; struct Address address; };
Accessing Members of Nested Structures
To access members of a nested structure, use the dot (.) operator for each level of nesting.​
struct Book b1; strcpy(b1.title, "Understanding C"); strcpy(b1.writer.name, "Sanfoundry Expert"); b1.writer.birthYear = 1975;
If using pointers:​
struct Book *ptr = &b1; strcpy(ptr->title, "Mastering C"); strcpy(ptr->writer.name, "Board Expert"); ptr->writer.birthYear = 1985;
Initializing Nested Structures
1. Separate Initialization
Initialize the nested structure first, then assign it to the outer structure.
struct Author auth = {"Scaler Author", 1980}; struct Book b = {"C Guide", auth};
2. Combined Initialization
Initialize both outer and nested structures at once.
struct Book b = {"Learn C Fast", {"Paul Priddle", 1990}};
Passing Nested Structures to Functions
Passing the Entire Structure
void printBook(struct Book b) { printf("Author: %s\n", b.writer.name); }
Passing Individual Members
void printAuthor(char *name) { printf("Author Name: %s\n", name); }
Example: Nested Structure in C
#include <stdio.h> #include <string.h> struct Author { char name[50]; int birthYear; }; struct Book { char title[100]; struct Author writer; // Nested structure }; int main() { struct Book b1; // Assign values strcpy(b1.title, "Understanding C"); strcpy(b1.writer.name, "Sanfoundry Expert"); b1.writer.birthYear = 1975; // Display data printf("Book Title: %s\n", b1.title); printf("Author: %s\n", b1.writer.name); printf("Author Birth Year: %d\n", b1.writer.birthYear); return 0; }
Output
Book Title: Understanding C Author: Sanfoundry Expert Author Birth Year: 1975
This C program uses nested structures to store and display information about a book and its author. The Author structure holds the author’s name and birth year, while the Book structure includes the book’s title and an Author object. In the main() function, values are assigned to a Book variable and then printed. It shows how to organize related data clearly and simply.
Advantages of Nested Structures
- Improved Data Organization: Nested structures allow for better organization of related data, grouping logically related attributes together, making the code more readable and maintainable.
- Code Reusability: Nested structures enable reuse of substructures in multiple places, reducing redundancy and potential errors in code.
- Better Data Abstraction: They help in abstracting complex real-world relationships into manageable components, making it easier to represent entities with hierarchical or composite attributes.
- Better Memory Management: Memory allocation in nested structures can be more efficient, reducing fragmentation and ensuring better memory alignment.
- Data Integrity: Keeping related data together within nested structures helps maintain the integrity and logical grouping of data, reducing the chances of mixing unrelated fields.
- Increased Flexibility: They offer flexibility in organizing multi-level or hierarchical data, allowing structures to be nested within each other, which is useful for representing complex relationships.
Limitations of Nested Structures
- Complexity: Deep nesting can make code harder to read and maintain.​
- Memory Overhead: May lead to increased memory usage if not managed properly.​
- Limited Flexibility: Embedded structures cannot be used independently outside the parent structure.
Sanfoundry Global Education & Learning Series – 1000 C Tutorials.
- Apply for C Internship
- Check C Books
- Apply for Computer Science Internship
- Practice Computer Science MCQs
- Watch Advanced C Programming Videos