Chapter 10 Structure and Unions
Chapter 10 Structure and Unions
Chapter 10 Structure and Unions
memory until they are associated with the structure variables. The general syntax of declaring structure
type variables is as follows:
struct tag_name svar1, svar2, ........svarn ;
e.g. struct student student1, student2 ;
Here, student1, student2 are structure type variables whose structure is given by student. It is also
possible to combine both the template declaration and variables declaration in one statement. Consider
an example:
struct employee
{
char emp_no[5] ;
char name [20] ;
float salary ;
} emp1, emp2, emp3 ;
Generally, structure definitions appear at the beginning of the program file, before any
variables or functions are defined. They may also appear before the main. In such cases, the definition is
global and can be used by other functions as well.
Structure Initialization: Like any other data type, a structure variable can be initialized at the time of
declaration. The format used is quite similar to the initialization of arrays. Consider the following
example:
struct student
{
char name[20] ;
int age ;
float avg ;
};
struct student st1 = {“Sabhya”, 5, 805} ;
Here, st1 is a structure type variable, which is assigned some initial values at the time of
declaration. Some more variations are also possible in initializing a structure type variable.
Following set of statements are also valid for initializing structure variables:
(i) struct student
{
char name[20] ;
int age ;
float avg ;
} st1 = {“Sabhya”, 5, 805} ;
C language does not permit the initialization of individual structure member within the
template. The initialization must be done only in the declaration of the actual variables.
Ganesh K. Sethi, Multani Mal Modi College, Patiala -3-
Storage of Structures: When a structure data type is declared in a program then memory is not
allocated to it. Memory is allocated only, when a structured variable is declared. The members of a
structure variable are stored in consecutive memory locations. The total memory allocated to a structure
variable is equal to the sum of memory allocated for the individual members of that structure. Each
member of structure variable is allocated memory in order of their respective declarations inside
structure template. Consider the following example:
struct employee
{
int code ;
char name [20] ;
float salary ;
}x;
Here x is a structure variable, whose members will be stored in a block of 26 consecutive
memory locations. The different members of x will be stored in memory in the following way:
xcode xname xsalary
Array Structure
(i) An array is a collection of data items of (i) A structure is a collection of data items
homogeneous data type i.e. of same of heterogeneous data types i.e. of
type. different types.
(ii) It has declaration only. (ii) It has declaration and definition.
(iii) There is no keyword in using arrays. (iii) The keyword struct is used.
(iv) An array name represents the address of (iv) A structure name is known as tag. It is a
the starting element. short hand notation of the declaration. It
does not represent the address of the
starting element.
(v) An array cannot have bit fields. (v) A structure may contain bit fields.
Unions: Unions, like structures, are also used to group a number of variables of different types in a
single unit. However, there is a major difference between them in terms of storage. Each member within a
structure has its own unique storage area, whereas all members of union share the same storage area
within the computer‟s memory. The compiler allocates sufficient memory to hold the largest member
type in the union. Since all the members of union occupy the same memory location, the value of the
previous member will be destroyed, when a value is assigned to another member of the union.
Ganesh K. Sethi, Multani Mal Modi College, Patiala -7-
(i) Union Declaration: The syntax for declaring a union and declaring variables of union type are same
except that instead of struct keyword, union keyword is used. Consider the following declaration of
union:
union sample
{
int i ;
float f ;
char c ;
} s1 ;
Here, sample is the name of the union and it has three data members i, f and c; s1 is declared
as union variable of type sample. In the above example, the compiler allocates a block of four consecutive
bytes sufficient enough to hold the largest data item as specified in the union. Out of three data members
i, f and c, only one member can be stored at a time. When i is assigned some value, it will be stored in first
two bytes, the other two bytes will be unused.
Similarly c will be stored in first byte and f will be stored in all four bytes of the block. The
sharing of a block of four consecutive memory locations by all members of a union is shown in the
following figure:
1201 1202 1203 1204
i
f
union
{
char color ;
int size ;
} shirt ;
printf(“Memory requirement of union is %d bytes\n”, size of (shirt)) ;
shirt.color = „b‟ ;
printf(“Color of shirt is %c\n”, shirt.color) ;
printf(“Size of shirt is %d\n”, shirt.size) ;
shirt.size = 40 ;
printf(“Size of shirt is %d\n”, shirt.size) ;
getche( ) ;
}
Execution of the above program results in the following output:
Memory requirement of union is 2 bytes
Color of shirt is b
Size of shirt is 28002
Size of shirt is 40
The third line of the output gives some garbage value, because we are trying to access
that union member which is not currently stored. The „sizeof‟ operator is giving the size of union in terms
of bytes. Here, the largest data item of union is of int type, so „sizeof‟ returns 2.
(iv) Applications of Union: Unions are used to conserve memory economically. They are useful for
those applications in which a grouped data item has a number of members and values are not
assigned to all members simultaneously.
(v) Difference between structure and union:
Structure Union
(i) Every member of a strcuture has its (i) All members of a union share the same
own memory. memory.
(ii) Keyword struct is used. (ii) Keyword union is used.
(iii) All members of a structure may be (iii) At a time, only one member of a union
given some initial values may be initialized.
simultaneously.
(iv) Different interpretations of the same (iv) Different interpretations for the same
memory location are not possible. memory location are possible.
(v) It consumes more memory space as (v) Memory is consumed economically.
compared to union.
Ganesh K. Sethi, Multani Mal Modi College, Patiala -9-