Tuesday 30 January 2018

Structures and Union

Structures and 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 :


----------------------------------------------------------------------------------------------------------------------------


 

----------------------------------------------------------------------------------------------------------------------------

Output :
 
Roll Number : 1
Name : Anjali Marks : 92.500000
Roll Number : 2
Name : Rahul Marks : 87.000000

----------------------------------------------------------------------------------------------------------------------------
 
           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. 

----------------------------------------------------------------------------------------------------------------------------


 

----------------------------------------------------------------------------------------------------------------------------



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 :

----------------------------------------------------------------------------------------------------------------------------


 

----------------------------------------------------------------------------------------------------------------------------

Output :
 
Employee Name : Rahul
Employee Age : 24
Employee Salary : 25000.939453

----------------------------------------------------------------------------------------------------------------------------

           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 : 

----------------------------------------------------------------------------------------------------------------------------


 

----------------------------------------------------------------------------------------------------------------------------

Output :
 
101 72000.671875
101 72000.671875

----------------------------------------------------------------------------------------------------------------------------

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 : 

----------------------------------------------------------------------------------------------------------------------------


 

----------------------------------------------------------------------------------------------------------------------------

Output :
 
a : 5
a : 1089051034
b : 7.300000

----------------------------------------------------------------------------------------------------------------------------

No comments:

Post a Comment