C++ Nested Structure with Example

A structure element (member) may be either complex or simple. The simple elements can be any of C++'s fundamental data types, such as int, float, char, or double. However, a structure may consist of an element that itself is complex, e.g., arrays, structures, etc.

Thus, an element of a structure may have been an array or a structure in itself. A structure consisting of such complex elements is called a complex structure.

C++ Nested Structure Definition

A structure can be nested inside another structure. The following code fragment illustrates it:

struct addr
{
   int houseno;
   string area;
   string city;
   string state;
};
struct emp
{
   int id ;
   string name;
   addr address;            // complex element
   float basic;
};

The structure "emp" has been defined as having several elements, including a structure variable "address" as well. The element "address" (of structure "emp") is itself a structure of type "addr." Therefore, inside the structure "emp," the following statement

addr address;

declares a variable "address" of the type "addr."

Please note: While defining such structures, just make sure that inner structures are defined before outer structures.

C++ Accessing Nested Structure Member

The members of structures are accessed using the dot operator. To access the "address" member of the "emp" structure, which is a variable of another structure, "addr", we shall write:

e.address.houseno

where "e" is the "emp" structure variable, "address" is a complex element or a member of the "emp" structure, and "houseno" is a member of the other structure named "addr."

We can write the following to set up the "houseno" member of the "addr" structure, whose variable ("address") is declared when the "emp" structure is declared:

e.address.houseno = 1693;

where "e" is the "emp" structure variable. The example given below will clear all your doubts regarding the nested structure in C++.

C++ Nested Structure Example

The idea of nested structures can be seen in action in the following example program, which was written in C++.

#include<iostream>
using namespace std;

struct addr
{
   int houseno;
   string area;
   string city;
   string state;
};
struct emp
{
   int id ;
   string name;
   addr address;            // complex element
} ;

int main()
{
   emp e;
   
   cout<<"Enter the ID: ";
   cin>>e.id;
   cout<<"Enter the Name: ";
   cin>>e.name;
   cout<<"Enter the House No.: ";
   cin>>e.address.houseno;
   cout<<"Enter the Area: ";
   cin>>e.address.area;
   cout<<"Enter the City: ";
   cin>>e.address.city;
   cout<<"Enter the State: ";
   cin>>e.address.state;

   cout<<"\n\nEmployee ID: "<<e.id;
   cout<<"\nEmployee Name: "<<e.name;
   cout<<"\nAddress: ";
   cout<<e.address.houseno<<", "<<e.address.area<<", "<<e.address.city<<", "<<e.address.state;
   cout<<endl;

   return 0;
}

The following snapshot shows the initial output produced by the above program.

c++ nested structure example

Now enter the following inputs: "12435" as the ID and press the ENTER key, "William" as the name and press the ENTER key again, "304" as the House Number and press the ENTER key, "Kingwood" as the area and press the ENTER key, "Houston" as the city and press the ENTER key, and "Texas" as the state and press the ENTER key to produce the output shown below.

c++ nested structure program

The following statement is from the preceding example:

e.address.area;

accessed the "area," the member of the "addr" structure whose variable ("address") is declared inside the "emp" structure. Therefore, since I declared "e" as the structure variable of the "emp" structure, the above statement refers to the "area," a member of the "addr" structure.

C++ Quiz


« Previous Tutorial Next Tutorial »


Liked this post? Share it!