Structures in C
A structure is a user defined data type in C/C++. A structure creates a data type that can be used to group items of possibly different types into a single type.
Suppose if we want to store the employee details like name, age, department and gender we can create a user-defined type which is structure.
Structure will allocate individual address to each of the member variables.
Define a Structure
The struct
keyword is used to define a structure. The struct
defines a new data type which is a collection of primary and derived data types.
Syntax
struct [structure_tag]
{
//member variable 1
//member variable 2
//member variable 3
...
}[structure_variables];
To create a structure we use struct
keyword and provide a tag name to it as an identifier. Now inside the body of the struct
we define the member variables and after closing the struct
, we can specify one or more structure variables. This is optional.
Example
struct Employee
{
char name[25];
int age;
char department[10];
char gender;
};
Here the structure name is Employee with variables name, age, department and gender.
Declaring Structure variable
Method 1
struct Employee
{
char name[25];
int age;
char department[10];
char gender;
};
// declaring variables of struct Employee
struct Employee emp1, emp2;
Method 2
struct Employee
{
char name[25];
int age;
char department[10];
char gender;
}emp1,emp2;
The recommended method to use structure is the method 1.
How to access Structure Members:
Structure members have no meaning individually without the structure. In order to assign a value to any structure member, the member name must be linked with the structure variable using a dot .
operator also called period or member access operator.
Example
#include<stdio.h>
#include<string.h>
struct Employee
{
char name[25];
int age;
char department[10];
char gender;
};
int main()
{
struct Employee emp1;
// using string function to add name
printf("Enter employee name\n");
scanf("%s", emp1.name);
printf("Enter employee age\n");
scanf("%d", emp1.age);
printf("Enter employee gender\n");
scanf("%c", emp1.gender);
printf("Employee Name %s\n", emp1.name);
printf("Employee Age: %d\n", emp1.age);
printf("Employee Gender: %c\n", emp1.gender);
return 0;
}
Output
Enter employee name
Andrew
Enter employee age
25
Enter employee gender
M
Employee Name:
Andrew
Employee Age:
25
Employee Gender:
M
Array of Structures
Suppose if we need to get the details of multiple employees, instead of creating multiple structure variables we can create array of structures.
Syntax
struct Employee emp[10];
Now each element of array emp
is of type Employee.
Example
#include<stdio.h>
#include<string.h>
struct Employee
{
char name[25];
int age;
char gender;
};
int main()
{
struct Employee emp[5];
for(int i=0;i<5;i++;)
{
// using string function to add name
printf("Enter employee name\n");
scanf("%s", emp1[i].name);
printf("Enter employee age\n");
scanf("%d", emp1[i].age);
printf("Enter employee gender\n");
scanf("%c", emp1[i].gender);
}
printf("Employee Records");
for(int j=0;j<5;j++)
{
printf("Employee Name %s\n", s1.name);
printf("Employee Age: %d\n", s1.age);
printf("Employee Gender: %c\n", s1.gender);
printf("-------------------");
}
return 0;
}