Structures and Unions
Structures and Unions
Introduction to Structure
A structure is a collection of one or more variables, possibly of different data types (e.g., int, float, char), grouped under a single name for convenient handling. Structures help to organize data, particularly in large programs, because they allow a group of related variables to be treated as a single unit.
The keyword struct introduces a structure declaration, which is a list of declarations enclosed in braces.
struct employee
{
char name[35];
unsigned int age;
char address[50];
float salary;
} emp1, emp2;
Initialization of Structure
Like other variables and arrays, structure variables can also be initialized where they are declared. To initialize structure variables, we list the values for the individual members separated by commas and enclosed in braces.
struct employee
{
char name[35];
unsigned int age;
char address[50];
float salary;
} emp1={"alpha",20,"Kathmandu",15000.00}, emp2={"beta", 25,"Bhaktapur", 20000.00};
struct employee emp3={"gamma",25,"Lalitpur", 25000.00}
Size of Structure
A structure is a collection of multiple variables of different types. The compiler automatically does the structure padding to ensure all its members are byte-aligned, which means they are always stored in a continuous memory location.
struct employee
{
char name[35];
unsigned int age;
char address[50];
float salary;
}
In the above program, as we know char takes 1 byte and since it is a character array of size 35 so 1 character takes 1 byte, i.e, 35 bytes.
Accessing a member of a structure
Structure members are accessed using dot[.] operator which is also called “Structure member Operator”.
Suppose;
printf("The employee's name is %s",emp1.name);
Array of Structure
A collection of similar types of structures placed in a common variable name is called an array of structure.
C Program to store a record of 100 books.
#include<stdio.h>
#include<conio.h>
struct book
{
char name[50];
float price;
int pages;
};
void main()
{
struct book b[100];
float temp;
int i;
for(i=0;i<100;i++)
{
printf("\nEnter name:");
scanf("%s", b[i].name);
printf("\nEnter price:");
scanf("%f",&temp);
b\[i\].price=temp;
printf("\nEnter pages:");
scanf("%d", &b[i].pages);
}
for(i=0;i<100;i++)
{
printf("\nName:%s\tPrice:%f\tPages:%d",b[i].name,b[i].price,b[i].pages);
}
getch();
}
Difference Between Array and Structure:
Array | Structure |
---|---|
Array is a collection of homogeneous data. | Structure is a collection of heterogeneous data. |
It allocates static memory. | It allocates dynamic memory. |
We cannot have array of array. | We can have array of structure. |
It cannot take part in complex data structure. | It can take part in complex data structure. |
Syntax: data_type array_name[size]; | Syntax:struct structure_name{datatype member1;datatype member2;….. datatype memberN; }; |
Union: Definition and Declaration
A union is a user-defined type similar to structure except for one key difference. A union is a special data type available in C that allows storing different data types in the same memory location.
The format of the union statement is as follows:
union union_name
{
member definition;
member definition;
...
member defination;
} union_variables;
Difference Between:
Union | Structure |
---|---|
The keyword union is used to declare a union. | The keyword struct is used to declare a structure. |
Memory size of the union is equal to the size of the largest member. | Memory size of the structure is equal to sum of size of each member. |
It cannot take part in the complex data structure. | It can take part in the complex data structure. |
Professionally specialized to interact with hardware. | It cannot be used to interact with the hardware in all aspects. |
Memory allocation is performed by sharing the memory with the highest data type. | Memory allocation of every element is independent of each other. |