< Prev
Next >

# C - Array of Structure

As we have know that by using structure, C allow us to create a data type, which can be a collection of different primitive data type elements such as int, char, float, double etc. But, do you also know that you could even have an array of structure? Yes, it is possible. Let's understand the concept with an example.

Let's suppose, we have to store a collection of information about not one but multiple employees, such as -
• The name of an employee in char[] array.
• The age of an employee in int data type.
• The salary of an employee in float data type.

In such situation, we would have to create an array of structure to create a data type to hold multiple structure variables, where each structure variables is collection of different data types mentioned above. Let's understand such an array of structure with an example.

• ## Understanding array of structure by an example

• In this example, we have created a structure named bag which has three elements of different type, such as -
• A char variable named gender_type.
• A int variable named year_of_make.
• A float variable named price.

Instead of create a single variable of such bag structure type, we will create an array of variables of bag structure type.

``````/* An example of array of structure */

#include<stdio.h>

int main()
{

struct bag
{
char gender_type;
int year_of_make;
float price;
};

struct bag b1[2];

for(int i=0;i<2;i++)
{
printf("Enter the details of %d  bag",(i+1));
printf("\n");

printf("Enter the year of make of the bag :");
scanf("%d", &b1[i].year_of_make);
getchar(); /* using getchar() function is important here, as it consumes the enter pressed after entering an int value, */
/* which doesn't bother next scanf() function from taking a char value */

printf("Enter the  gender type of the bag :");
scanf("%c", &b1[i].gender_type);

printf("Enter the price of the bag :");
scanf("%f", &b1[i].price);
}

for(int i=0;i<2;i++)
{
printf("The gender type of the bag : %c", b1[i].gender_type);
printf("\n");

printf("The year of make of the bag : %d", b1[i].year_of_make);
printf("\n");

printf("The price of the bag :  : %.2f", b1[i].price);
printf("\n");
}

return 0;
}
```
```

## Output is :

``````Enter the details of 1  bag
Enter the year of make of the bag :2017
Enter the  gender type of the bag :M
Enter the price of the bag :9.8
Enter the details of 2  bag
Enter the year of make of the bag :2018
Enter the  gender type of the bag :F
Enter the price of the bag :10.9
The details of 1  bag
The gender type of the bag : M
The year of make of the bag : 2017
The price of the bag :  : 9.80
The details of 2  bag
The gender type of the bag : F
The year of make of the bag : 2018
The price of the bag :  : 10.90
```
```

## Program Analysis

• In this code above, we have created a structure named bag, which is a collection of different data types including an array element. This structure can also be termed as a

``````struct bag
{
char gender_type;
int year_of_make;
float price;
};
``````

• The structure we declare is also an user-defined data type which can be used to create variables of its type. We have created an array of variables named b1 of structure type bag by using the struct keyword with the name of the structure, such as -

`` struct bag b1[2]; ``

• Next, we have used a for-loop to initialize the array of variables, using the index of each variable represented by i with dot-operator and its elements in scanf() function. For example -

``````printf("Enter the year of make of the bag :");
scanf("%d", &b1[i].year_of_make);``````

• Finally, we have used another for-loop to display the value of the array of variables, using the index of each variable represented by i and its elements in printf() function. For example -

``printf("The gender type of the bag : %c", b1[i].gender_type);``

< Prev
Next >
< Structure with array element
Console I/O Functions >