codescracker


c++

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 :

worker.address.city

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++ */

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<stdio.h>

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;
}worker;

void main()
{
	clrscr();

	emp evar;

	cout<<"Employee No: ";
	cin>>evar.empno;
	cout<<"Employee Name: ";
	gets(evar.name);
	cout<<"Designation: ";
	gets(evar.desig);
	cout<<"House No: ";
	cin>>evar.address.houseno;
	cout<<"Street: ";
	gets(evar.address.street);
	cout<<"City: ";
	gets(evar.address.city);
	cout<<"State: ";
	gets(evar.address.state);
	cout<<"Basic Pay: ";
	cin>>evar.basic;
	cout<<"\n";

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

	getch();
}

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, worker.name will have "Ravi", worker.desig will be "Manager", worker.address.houseno will be "451", worker.address.area will be "Patel Nagar", worker.address.city" will be "Varanasi", worker.address.state will be "UP" and worker.basic will be 9800.00.


« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test