C
  • Introduction
    • Fundamentals of a Program
    • Overview of C
    • Features of C
  • Installing Required Software
    • Setting Up VSCode for Windows
    • Setting Up VSCode for macOS
    • Setting Up VSCode for Ubuntu
  • Starting to write code
    • Compiling and Running Your Code
    • Creating Our First C Program
    • Errors and Warnings
    • Program: Writing a C Program to Display Your Name
    • Structure of a C Program
  • Basic Concepts
    • Comments in C
    • Preprocessor in C
    • The #include Statement
    • Displaying Output
    • Reading Input from the Terminal
    • Enums and Chars
    • Data Types and Variables
    • Format Specifiers
    • Command Line Arguments
    • Program: Calculating the Area of a Triangle
  • Operators
    • Converting Minutes to Years and Days
    • Basic Operators
    • Bitwise Operators
    • Program: Byte Sizes of Basic Data Types
    • cast and sizeof Operators
    • Operator Precedence
  • Control Flow
    • If-Else Statements
    • Program: Weekly Pay Calculation
    • Switch Statement
    • For Loop
    • While and Do-While Loops
    • Nested Loops and Loop Control
    • Program: Guess the Number
  • Arrays
    • Introduction to Arrays
    • Program: Prime Number Generator
    • Multidimensional Arrays
    • Program: simple Weather Program
    • Variable Length Arrays (VLAs)
  • Functions
    • Overview of Functions
    • Defining Functions
    • Arguments and Parameters
    • Returning Data from Functions
    • Variable Scoping
    • Program: Tic Tac Toe Game
    • Recursion
  • Strings
    • Defining a String
    • Constant Strings in C
    • Common String Functions
    • Program: Bubble Sort
    • Searching, Tokenizing, and Analyzing Strings
    • Converting Strings
  • Debugging
    • What is Debugging
    • Understanding the Call Stack
    • Common C Mistakes
    • Understanding Compiler Errors
  • Pointer
    • Defining Pointers
    • Accessing Pointers
    • Program: Pointer Demonstration
    • Pointers and Const
    • Void Pointers
    • String Pointers
    • Array Pointers
    • Utilizing Pointers with Functions
    • Pointer Arithmetic
  • Dynamic Memory Allocation
    • malloc, calloc, and realloc
    • Program: User Input String
    • Memory Deallocation
  • Structure
    • Structures and Arrays
    • Nested Structures
    • Structures and Pointers
    • Structures and Functions
    • Program: Structure pointers and Functions
  • File Input and Output
    • Accessing Files
    • Reading from a File
    • Program: Finding the Total Number of Lines in a Text File
    • Writing to a Text File
    • Finding Your Position in a File
    • Program: Converting Characters in a File to Uppercase
    • Program: Printing the Contents of a File in Reverse Order
  • The Standard C Library
    • Various Functions in C
    • Math Functions in C
    • Utility Functions in C
Powered by GitBook
On this page
  • Defining Nested Structures
  • Initializing Nested Structures
  • Accessing Members of Nested Structures
  • Array of Structures with Nested Structures

Was this helpful?

  1. Structure

Nested Structures

Nested structures in C allow you to create more complex data structures by incorporating one structure within another. This is useful for representing hierarchical relationships or organizing related information. Let's delve into the concept of nested structures:

Defining Nested Structures

  • A nested structure is a structure that is a member of another structure. This can create a hierarchy of data.

    struct Address {
        char street[50];
        char city[30];
        int zipCode;
    };
    
    struct Employee {
        char name[50];
        int empID;
        struct Address address; // Nested structure
    };

In this example, Employee structure contains another structure, Address.

Initializing Nested Structures

  • When initializing a nested structure, you need to initialize the outer structure first, followed by the inner structure.

    struct Employee employee1 = {"John Doe", 101, {"123 Main St", "Cityville", 12345}};

Accessing Members of Nested Structures

  • To access members of nested structures, use the dot (.) operator consecutively.

    printf("Employee Name: %s\n", employee1.name);
    printf("Employee ID: %d\n", employee1.empID);
    printf("Employee Address: %s, %s, %d\n", employee1.address.street, employee1.address.city, employee1.address.zipCode);

Array of Structures with Nested Structures

  • You can create an array of structures with nested structures.

    struct Employee employeesArray[3] = {
        {"Alice Wonderland", 201, {"456 Oak St", "Townsville", 54321}},
        {"Bob Smith", 202, {"789 Pine St", "Villageville", 67890}},
        {"Eve Johnson", 203, {"101 Cedar St", "Hamletville", 98765}}
    };

This array contains three Employee structures with nested Address structures.

Nested structures provide a way to model real-world entities with interconnected attributes. They improve code organization and readability, making it easier to work with complex data structures.

If you have specific questions or if there are additional topics you'd like to explore, feel free to ask!

PreviousStructures and ArraysNextStructures and Pointers

Was this helpful?