Why use structure?
In C, there are cases where we need to store multiple attributes of an entity. It is not necessary
that an entity has all the information of one type only. It can have different attributes of
different data types. For example, an entity Student may have its name (string), roll number
(int), marks (float). To store such type of information regarding an entity student, we have the
following approaches:
o Construct individual arrays for storing names, roll numbers, and marks.
o Use a special data structure to store the collection of different data types.
What is Structure
Structure in c is a user-defined data type that enables us to store the collection of different
data types. Each element of a structure is called a member. Structures ca; simulate the use of
classes and templates as it can store various information
The ,struct keyword is used to define the structure. Let's see the syntax to define the structure
in c.
struct structure_name
{
data_type member1;
data_type member2;
.
.
data_type memeberN;
};
Let's see the example to define a structure for an entity employee in c.
struct employee
{ int id;
char name[20];
float salary;
};
The following image shows the memory allocation of the structure employee that is defined
in the above example.
Declaring structure variable
We can declare a variable for the structure so that we can access the member of the structure
easily. There are two ways to declare structure variable:
1. By struct keyword within main() function
2. By declaring a variable at the time of defining the structure.
1st way:
Let's see the example to declare the structure variable by struct keyword. It should be
declared within the main function.
struct employee
{ int id;
char name[50];
float salary;
};
Now write given code inside the main() function.
1. struct employee e1, e2;
The variables e1 and e2 can be used to access the values stored in the structure. Here, e1 and
e2 can be treated in the same way as the objects in C++ and Java.
2nd way:
Let's see another way to declare variable at the time of defining the structure.
struct employee
{ int id;
char name[50];
float salary;
}e1,e2;
Which approach is good
If number of variables are not fixed, use the 1st approach. It provides you the flexibility to
declare the structure variable many times.
If no. of variables are fixed, use 2nd approach. It saves your code to declare a variable in
main() function.
Accessing members of the structure
There are two ways to access structure members:
1. By . (member or dot operator)
2. By -> (structure pointer operator)
Let's see the code to access the id member of p1 variable by. (member) operator.
1. p1.id
C Structure example
Let's see a simple example of structure in C language.
#include<stdio.h>
#include <string.h>
struct employee
{ int id;
char name[50];
}e1; //declaring e1 variable for structure
int main( )
{
//store first employee information
e1.id=101;
strcpy(e1.name, "Sonoo Jaiswal");//copying string into char array
//printing first employee information
printf( "employee 1 id : %d\n", e1.id);
printf( "employee 1 name : %s\n", e1.name);
return 0;
}
Output:
employee 1 id : 101
employee 1 name : Sonoo Jaiswal
Let's see another example of the structure in C language
to store many employees information.
#include<stdio.h>
#include <string.h>
struct employee
{ int id;
char name[50];
float salary;
}e1,e2; //declaring e1 and e2 variables for structure
int main( )
{
//store first employee information
e1.id=101;
strcpy(e1.name, "Sonoo Jaiswal");//copying string into char array
e1.salary=56000;
//store second employee information
e2.id=102;
strcpy(e2.name, "James Bond");
e2.salary=126000;
//printing first employee information
printf( "employee 1 id : %d\n", e1.id);
printf( "employee 1 name : %s\n", e1.name);
printf( "employee 1 salary : %f\n", e1.salary);
//printing second employee information
printf( "employee 2 id : %d\n", e2.id);
printf( "employee 2 name : %s\n", e2.name);
printf( "employee 2 salary : %f\n", e2.salary);
return 0;
}
Output:
employee 1 id : 101
employee 1 name : Sonoo Jaiswal
employee 1 salary : 56000.000000
employee 2 id : 102
employee 2 name : James Bond
employee 2 salary : 126000.000000
Union in C
Like Structures, union is a user defined data type. In union, all members
share the same memory location.
#include <stdio.h>
// Declaration of union is same as structures
union test {
int x, y;
};
int main()
{
// A union variable t
union test t;
t.x = 2; // t.y also gets value 2
printf("After making x = 2:\n x = %d, y = %d\n\n",
t.x, t.y);
t.y = 10; // t.x is also updated to 10
printf("After making y = 10:\n x = %d, y = %d\n\n",
t.x, t.y);
return 0;
}
Output:
After making x = 2:
x = 2, y = 2
After making y = 10:
x = 10, y = 10
Structure Union
You can use a struct keyword to define a
You can use a union keyword to define a
structure.
union.
Every member within structure is
In union, a memory location is shared by all
assigned a unique memory location.
the data members.
Changing the value of one data member
Changing the value of one data member will
will not affect other data members in
change the value of other data members in
structure.
union.
It enables you to initialize several
It enables you to initialize only the first
members at once.
member of union.
The total size of the structure is the sum The total size of the union is the size of the
of the size of every data member. largest data member.
It is mainly used for storing various data
It is mainly used for storing one of the many
types.
data types that are available.
It occupies space for a member having the
It occupies space for each and every
highest size written in inner parameters.
member written in inner parameters.
You can access one member at a time in the
You can retrieve any member at a time. union.
It supports flexible array. It does not support a flexible array.