Structures & Union
Structures & Union
The C language allows us to create custom data types. The structure is a custom data type which c
combines different data types .The structure is a custom data type which combine different data types to
form a new user define data type.
Structure definition
A structure is a collection of variable reference under one name providing a convincible means of related
information together.
Format:
struct tag_name
{
data _type member1;
data_type member2;
-------------------
---------------------
};
here a keyboard struct declares a structures to hold the details of field of different data types.
Example:
struct addr
{
char name [30];
char city [15];
int pincode ;
};
Creating Structure variable
Structure variable can be created in two ways:
1. Declaration using tag_name anywhere in the program.
Example:
struct book
{
char name [30];
char author [25];
float price;
};
struct book book1, book2
2. it is also allowed to combine structure declaration and variable declaration in one statement.
Example:
struct person
{
char *name;
int age;
char*address;
} p1, p2, p3;
Image showing how the given value allocate in structure with the help of an example >
Arrays of Structures
The most common use of structures is in arrays of structures. To declare an array of structures, first the
structure is defined then an array variable of that structure is declared.
Example:
struct class student [100];
It defines an array called student which consists of 100 elements of structure named class.
Array within structures
Single as multidimensional arrays of type int and float can be defined as structure members.
Example:
struct marks
{
int number;
float subject[3];
}
student [2];
Here the member subject contains three elements, subject[0], subject[1] and subject[2] there elements
can be accessed using appropriate subscript. For instance, the name student [1] student [2]; would refer
to the marks obtained in the third subject by the secured student.
Structures within structures
Structures within a structure means nesting of structures.
Example:
struct salary
{
char name [20];
char department [10];
int basic-pay;
int dearness-allowance;
int house_rent_allowance;
int city_allowance;
} employee;
This structure defines name, department, basic pay and three kinds of allowances. All the items related
to allowance can be grouped together and declared under a sub-stricture. As shown below,
struct salary
{
char name []2;
char department [10];
struct
{
int dearness;
int house_rent;
int city;
}allowance;
} employee;
The salary structure contains a member named allowance which use is a structures with. Three
members. Now ; the member compared in the inner structure;, namely, ;dearness, house_rent and city
can ;be left to as;
employee.allowance.dearness
employee.allowance.house_rent
employee.allowance.city
The inner most member in a nested structure can be accessed by chaining all the concerned structure
variables (from outermost to inner most) with the member using dot operator.
union tag_name
{
member 1;
member 2;
…..
member n;
};
Where union is required keyword and the other terms have the same meaning as in a structure
definition.
Individual union variables can then be declared as:
storage-class union tag variable 1, variable 2, . . . , variable n;
Where storage-class is an optional storage class specified, union is a required keyword, tag is the name
that appears in the union definition, and variable 1, variable 2, . . . , variable n are union.
The two declarations may be combined, just as we did with structures. Thus, we can write Storage-class
union tag
{
member 1;
member 2;
.....
member n;
}
Uses of Union
Union, like structure, contain members whose individual data type may differ to each other.
But the members that compose a union share the same storage area within the computer's memory,
whereas each member within a structure is assigned its own unique storage area.
Thus, union are used to conserve memory.
1. Unions are useful for application involving multiple members, where value need not to be assigned to
all of the members at any one time.
2. Unions are useful whenever there is a requirement to access the same memory location in more than
one way. etc.
Difference between Structure & Union
Structure Union
Keyword The struct keyword is used while The union keyword is used while
defining structure defining union
Initialization The members of the structures can All the members of the union cannot
be initialized simultaneously be initialized simultaneously only first
member of the union can be
initialized
Memory In structure, all the members has its In union, all the members share same
location own memory location memory location
Size The memory allocated for structure The compiler allocates memory for
variable by the compiler, is greater the union by considering the size of
than or equal to sum of the sizes of largest member so the size allocated
its members by union is equal to size of the largest
member