Lesson 10
Structures & Unions
1. Structure Definition
2. Structure Declaration
3. Initialization & Accessing Structure Members
4. Nesting of Structures
5. Union: Definition, Declaration & Initialization
6. Arrays of Structure & Unions
1. Structure Definition
A struct in the C programming language (and many derivatives) is a composite data type (or record) declaration that defines a physically grouped list of variables under one name in a block of memory, allowing the different variables to be accessed via a single pointer or by the struct declared name which returns the
2. Structure Declaration
A "structure declaration" names a type and specifies a sequence of variable values (called "members" or "fields" of the structure) that can have different types. ... Structures in C are similar to the types known as "records" in other languages.
3. Initialization & Accessing Structure Members
C language supports multiple ways to initialize a structure variable. You can use any of the initialization method to initialize your structure.
How to initialize a structure variable?
1. Initialize using dot operator.
2. Value initialized structure variable.
3. Variant of value initialized structure variable.
4. Nesting of Structures
Nested structure in C is nothing but structure within structure. One structure can be declared inside other structure as we declare structure members inside a structure. The structure variables can be a normal structure variable or a pointer variable to access the data.
5. Union: Definition, Declaration & Initialization
A union is also a user defined datatype and its declaration and initialization of its members is also same as that of structure. Accessing the members of the union is also same as that of structure except that struct keyword is replaced by union and union helps to save the memory allocated. The compiler allocates memory for the union by considering the size of largest member. In union, the members share same memory location. As the members of the union share memory only one member enters in the memory at a time and only that member is initialized while other member hold garbage value.
Syntax
union union_name
{
datatype member1;
datatype member2;
_ _ _ _ _
_ _ _ _ _
};
union union_name variable1;
OR
union union_name
{
datatype member1;
datatype member2;
_ _ _ _ _
_ _ _ _ _
}variable1;
The members of the structure can be accessed using a (.) operator if there is union variable, and we can use (->) operator if there is pointer to structure
Consider below program to understand union
#include<stdio.h>
union student //declaration of union
{
int rollno;
char nm[10];
int marks;
};
void main()
{
union student u1={01,“Mary”,65}; //Initializing the members of the union
printf(“Student details are:\n”);
printf(“\nRoll no: %d”,u1.rollno);
printf(“\nName: %s”,u1.nm);
printf(“\nMarks: %d”,u1.marks);
}
On compiling and executing above program following is the output
Student details are:
Roll no: 01
Name: Mary
Marks: 65
6. Arrays of Structure & Unions
Array in C :
An array is collection of similar data items accessed by a common name stored at continuous memory locations. The elements of an array can be accessed using indices. They can be used to store primitive data types such as int, float, double, char, etc. but all elements have to be of the same data type. Below is the picturesque representation of an array.
2. Union in C :
Union is a user defined datatype that allows storage of heterogenous elements in the same memory location. Size of the union is the size of the largest element in the union. Below is the picturesque representation of a union.
Difference between Array and Union :
ARRAY UNION
Collection of elements of same data types. Collection of elements of heterogenous data types.
Arrays can be one or two dimensional. Unions do not have type.
Each element is allocated a specific memory location. The elements share the memory location which has size equivalent to the size of the largest size element of the union.
All members can contain value at a given time. Only one member can contain value at a given time.
Not an efficient use of memory as all members are allocated different memory locations. Efficient use of memory as all members do not require separate memory location.
Array elements can be accessed using index. The elements of a union cannot be accessed using index.
Syntax :
datatype array_name[size] Syntax :
Union user defined name
{
datatype Variable 1; datatype variable2;
};