***Welcome to ashrafedu.blogspot.com * * * This website is maintained by ASHRAF***

Posts

    Showing posts with label Structure. Show all posts
    Showing posts with label Structure. Show all posts

    Thursday, November 26, 2020

    Array of Structures in C

    An array of structures in C can be defined as the collection of multiple structures variables where each variable contains information about different entities.

    The array of structures in C is used to store information about multiple entities of different data types.

    The array of structures is also known as the collection of structures.


    In the above given example, a structure of employee is defined which contain three members (id , name, salary).

    In the above example emp[2] variable has been declared, which means there is a collection of two employee structures.

     

    Example:

    Example of an array of structures that stores information of 5 students and prints it.

    #include<stdio.h> 

    #include <string.h>   

    struct student{   

    int rollno;   

    char name[10];   

    };   

    void  main(){   

    int i;   

    struct student st[5];   

    printf("Enter Records of 5 students");   

    for(i=0;i<5;i++){   

    printf("\nEnter Rollno:");   

    scanf("%d",&st[i].rollno);   

    printf("\nEnter Name:");   

    scanf("%s",&st[i].name);   

    }   

    printf("\nStudent Information List:");   

    for(i=0;i<5;i++){   

    printf("\nRollno:%d, Name:%s",st[i].rollno,st[i].name);   

    }       

    }    

    Structure

     A structure is a user defined data type in C. A structure creates a data type that can be used to group items of possibly different types into a single type.

    structure is user defined data type available in C that allows to combine data items of different kinds.

    A structure is a collection of heterogeneous (different) data items into a single type. Each element of a structure is called a member. 

    Defining a Structure

    struct keyword is used to define the structure.

    The syntax to define the structure in c :

    struct structure_name  

    { 

        data_type member1; 

        data_type member2; 

        . 

        . 

        data_type memeberN; 

    };

     

    Example:

     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.


    sizeof(employee) = 4+10+4 bytes = 18 bytes

    Declaring structure variable

    A variable for the structure has to be declared to 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.

    First Method:

     struct employee 

    { 

        int id; 

        char name[50]; 

        float salary; 

    };

     After defining structure write below code inside the main() function.

     struct employee e1, e2; 

     The variables e1 and e2 can be used to access the values stored in the structure.

     Second Method:

     struct employee 

    {  

        int id; 

        char name[50]; 

        float salary; 

    }e1,e2; 

    If number of variables is not fixed, use the first method. It provides the flexibility to declare the structure variable as many times as required

    Accessing members of the structure

    There are two ways to access structure members:

    1. By . (member or dot operator)
    2. By -> (structure pointer operator)

    A pointer can be created to a structure. If a pointer to structure is created, members are accessed using arrow ( -> ) operator.

    The code to access the id member of e1 variable by . (dot) operator.

    e1.id

    Example 1:

    #include<stdio.h> 

    #include <string.h>   

    struct employee     

    {   int id;     

        char name[50];     

    }e1;  //declaring e1 variable for structure   

    void  main( )   

    {   

       //store first employee information   

       e1.id=101;   

       strcpy(e1.name, "Ashraf");//copying string into char array   

       //printing first employee information   

       printf( "employee1 id : %d\n", e1.id);   

       printf( "employee1 name : %s\n", e1.name);   

    }  

    Example 2:

    #include<stdio.h>

    struct Point

    {

       int x, y;

    };

    void  main()

    {

       struct Point p1 = {1, 2};

       // p2 is a pointer to structure p1

       struct Point *p2 = &p1;

       // Accessing structure members using structure pointer

       printf("%d %d", p2->x, p2->y);

    }



    Top