Advertisement



< 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 -
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 -

    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







    Please share this article -





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



    Advertisement

    Please Subscribe

    Please subscribe to our social media channels for daily updates.


    Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




    Advertisement

    Notifications



    Please check our latest addition

    PYTHON and DJANGO


    Advertisement