[C++/C] Usage of Nested Structures | How to Structure Complex Data

目次

Introduction

Structures (struct) in C++ and C are convenient features for grouping related variables into one unit, but their true value is demonstrated when they are nested. You can have a variable of another structure type as a member of a structure, such as a large “Company” structure containing multiple “Employee” structures. This allows you to intuitively and cleanly represent complex real-world hierarchical structures within your program. In this article, I will explain in an easy-to-understand manner how to use nested structures and how to access members at the nested destination.

Sample Code for Nested Structures

This code first defines a basic structure called Staff. Next, it uses that Staff type to define a larger structure called Department.

Complete Code

#include <iostream>
#include <string>

// --- Definition of the basic "Staff" structure ---
struct Staff {
    int id;
    std::string name;
};

// --- Definition of "Department" structure holding "Staff" as members ---
struct Department {
    std::string departmentName; // Department Name
    Staff manager;              // Manager (Staff type)
    Staff leadStaff;            // Leader (Staff type)
};

int main() {
    // --- 1. Create a variable (instance) of the "Department" structure ---
    Department salesDept;
    
    // --- 2. Assign values to each member ---
    salesDept.departmentName = "Sales Department";
    
    // Accessing members of the nested structure
    salesDept.manager.id = 101;
    salesDept.manager.name = "Ichiro Suzuki";
    
    salesDept.leadStaff.id = 205;
    salesDept.leadStaff.name = "Hanako Sato";

    // --- 3. Reference and display member values ---
    std::cout << salesDept.departmentName << " Information:" << std::endl;
    
    std::cout << "Manager: "
              << salesDept.manager.name 
              << " (ID: " << salesDept.manager.id << ")" 
              << std::endl;
              
    std::cout << "Leader: "
              << salesDept.leadStaff.name 
              << " (ID: " << salesDept.leadStaff.id << ")" 
              << std::endl;

    return 0;
}

Execution Result

Sales Department Information:
Manager: Ichiro Suzuki (ID: 101)
Leader: Hanako Sato (ID: 205)

Code Explanation

struct Department { … };

The Department structure has a std::string type member departmentName, as well as Staff type members manager and leadStaff. In this way, you can use a custom structure as the type for a member of another structure, just like int or std::string.

salesDept.manager.id = 101;

This is the syntax for accessing members of a nested structure.

  • salesDept.manager: First, access the manager member of the salesDept variable (Department type). This manager member itself is a structure of Staff type.
  • .id: Next, access the id member of that manager structure.

By chaining dot operators (.) from left to right like this, you can reach members at the desired hierarchy level.

Summary

In this article, I explained how to use nested structures to represent more complex data structures.

  • You can define a variable of another structure type as a member of a structure.
  • To access nested members, chain dot operators (.).

This technique becomes more valuable as the data handled by the program becomes more complex. By modeling real-world objects and concepts within the program by combining structures, the code becomes very organized, intuitive, and easy to handle.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次