The best programs are written so that computing machines can perform them quickly and so that human beings can understand them clearly. A programmer is ideally an essayist who works with traditional aesthetic and literary forms as well as mathematical concepts, to communicate the way that an algorithm works and to convince a reader that the results will be correct. ― Donald E. Knuth

# C++ Tutorial

## Structures & Union

 Module Index ► Structures ► Array of Structures ► Nesting of Structures ► Pointer to structures ► Union

Structures
Suppose you want to store information about a student ( name, roll no., marks ). You need a character array or a string to store the name, integer variable to store roll no. and a float variable to store marks. To access the information, the string, int and float variables needs to be accessed separately. Structures provide a mechanism to combine all the information under a single data type. In simple words, we have seen atomic variables ( int, char, float, etc. ) storing single piece of information, arrays store multiple pieces of information of same data type, but structures can hold multiple pieces of information of dissimilar data types. See the following program to understand how structures are defined, initialized and how data in a structure is accessed :

Please note that s1 and s2 are variables of a user defined data type struct student. The members of struct student data type are name, roll and marks and they are accessed using dot ( . ) operator.

Array of Structures
In the above program, we stored information about 2 students in variables s1 and s2. What if you want to store information about 100 students ? We will simply use an array of structures. Following program demonstrate the use of array of structures. Also, it illustrates how array of structures is passed to a function. Structure variables are passed in the same way as variables of other data types are passed.

Run this program in your system to take input at run-time

Nesting of Structures
We can declare a structure variable inside another structure. This is known as nesting of structures. In order to access one of the members of nested structures, dot operator is used until the member is reached in the structure hierarchy. Following program illustrates the nesting of structures :

In the above program, structure person has been defined outside and a variable p of type person is declared inside structure employee. We can actually define the the complete structure inside another structure.

Pointer to structures
A pointer variable stores the address of some other variable. Same principle applies to structure variables also. We can declare a pointer which points to a structure variable and then the members of that structure can be accessed using the pointer. The only difference being the use of arrow ( -> ) operator while using pointers.
See the program below :

Union
Union is similar to a structure in terms of declaration and usage but all the members of a union occupy the same space in memory i.e, memory location is shared among the union members. The size of the union is
that of the greatest member in terms of size. Since all the members of a union share the same memory space, modification of one member affects other. The value of one member gets overwritten by the value of other member. See the program below :