Creating an Array of Structures in C


In C, an array of structures can be created just like any other array. Each element of the array is a structure of the same type.
For example, you can create an array of "student" structures like this:
        struct student
        {
             char name[30];
             int age;
             float per;
        }
   Example :
         struct student students[100];

  • In this example, the "students" array is an array of 100 elements, where each element is a "student" structure. Each element has three members: name, age, and per, which are defined as an array of characters, an integer, and a floating-point number, respectively.
  • You can access the elements of the array using the array index, and access the members of the structure using the dot operator (.).

Source Code

//Array of Structure Objects
 
#include<stdio.h>
struct student
{
    char *name;
    int age;
    float per;
};
int main()
{
    struct student o[2];
 
    o[0].name="Ram Kumar";
    o[0].age=25;
    o[0].per=65.25;
 
    o[1].name="Sam Kumar";
    o[1].age=12;
    o[1].per=80;
 
    printf("\n------------------------------");
    printf("\nName        : %s",o[0].name);
    printf("\nAge         : %d",o[0].age);
    printf("\nPercent     : %f",o[0].per);
    printf("\n------------------------------");
    printf("\nName        : %s",o[1].name);
    printf("\nAge         : %d",o[1].age);
    printf("\nPercent     : %f",o[1].per);
    printf("\n------------------------------\n\n");
 
    return 0;
}
 
To download raw file Click Here

Output

------------------------------
Name        : Ram Kumar
Age         : 25
Percent     : 65.250000
------------------------------
Name        : Sam Kumar
Age         : 12
Percent     : 80.000000
------------------------------

List of Programs


Sample Programs


Switch Case in C


Conditional Operators in C


Goto Statement in C


While Loop Example Programs


Looping Statements in C

For Loop Example Programs


Array Examples in C

One Dimensional Array


Two Dimensional Array in C


String Example Programs in C


Functions Example Programs in C