C++ Nested Data Structure

« Previous Tutorial Next Tutorial »

A structure element may be either complex or simple. The simple elements are any of the fundamental data types of C++ i.e., int, float, char, double. However, a structure may consist of an element that itself is complex i.e., it is made up of fundamental types e.g., arrays, structures etc.

Thus, an element of a structure may have be 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. Following code fragment illustrates it :

struct addr       // structure tag
   int houseno ;
   char area[26] ;
   char city[26] ;
   char state[26] ;
struct emp                    // structure tag
   int empno ;
   char name[26] ;
   char desig[16] ;
   addr address ;
         /* See, address is a structure variable itself (of type
           addr) and it is member of another structure,
           the emp structure. */

   float basic ;
} ;
emp worker ;       // create structure variable

The structure emp has been defined having several elements including a structure address also. The element address (of structure emp) is itself a structure of type addr.

Tip - While defining such structures, just make sure that inner structures are defined before outer structure.

C++ Accessing Nested Structure Member

The members of structures are accessed using dot operator. To access the city member of address structure which is an element of another structure worker, we shall write :

To initialize houseno member of address structure, element of worker structure, we can write as follows:

worker.address.houseno = 1693

As you can see, the elements of each structure are referenced from outermost to innermost.

C++ Nested Structure Example

Here is an example program, demonstrating the concept of nested structure in C++

/* C++ Nested Structure - Example of Nested Structure in C++ */


struct addr
   int houseno;
   char street[30];
   char city[20];
   char state[20];
struct emp
   int empno;
   char name[20];
   char desig[20];
   addr address;
   float basic;

void main()

   emp evar;

   cout<<"Employee No: ";
   cout<<"Employee Name: ";
   cout<<"Designation: ";
   cout<<"House No: ";
   cout<<"Street: ";
   cout<<"City: ";
   cout<<"State: ";
   cout<<"Basic Pay: ";

   char ch;
   cout<<"Want to see ? (y/n)...";
   if(ch=='y' || ch=='Y')
      cout<<"\nEmployee Data:\n";
      cout<<"Employee No: "<<evar.empno;
      cout<<"\nName: "<<;
      cout<<"\nDesignation: "<<evar.desig;
      cout<<"\nAddress: "<<evar.address.houseno<<", ";
      cout<<evar.address.street<<", ";
      cout<<<<", ";
      cout<<"Basic Pay: "<<evar.basic;


Here is the sample run of the above C++ program:

c++ nested data structure

The above program reads values into individual elements of nested structures worker. The function gets() has been used to read strings as strings may contain white spaces also and the ">>" operator ignores white spaces. However, if you want to initialize a nested structure at the time of its declaration, this can be achieved as follows :

emp worker = { 103, "Ravi", "Manager", {451, "Patel Nagar", "Varanasi", "UP"}, 9800.00};

See, while initializing a structure within structures the elements of inner structures have themselves been enclosed in { }.

The structure address of type addr which is embedded inside emp, is initialized separately by surrounding its values with braces. After the above statement, worker.empno will have value 103, will have "Ravi", worker.desig will be "Manager", worker.address.houseno will be "451", worker.address.area will be "Patel Nagar"," will be "Varanasi", worker.address.state will be "UP" and worker.basic will be 9800.00.

C++ Online Test

« Previous Tutorial Next Tutorial »

© Copyright 2021. All Rights Reserved.