• C Programming Video Tutorials

Array of Structures in C



In C programming, the struct keyword is used to define a derived data type. Once defined, you can declare an array of struct variables, just like an array of int, float or char types is declared. An array of structures has a number of use-cases such as in storing records similar to a database table where you have each row with different data types.

Usually, a struct type is defined at the beginning of the code so that its type can be used inside any of the functions. You can declare an array of structures and later on fill data in it or you can initialize it at the time of declaration itself.

Initializing a Struct Array

Let us define a struct type called book as follows −

struct book{
   char title[10];
   double price;
   int pages;
};

During the program, you can declare an array and initialize it by giving the values of each element inside curly brackets. Each element in the struct array is a struct value itself. Hence, we have the nested curly brackets as shown below −

struct book b[3] = {
   {"Learn C", 650.50, 325}, 
   {"C Pointers", 175, 225}, 
   {"C Pearls", 250, 250}
};

How does the compiler allocate memory for this array? Since we have an array of three elements, of struct whose size is 32 bytes, the array occupies "32 x 3" bytes. Each block of 32 bytes will accommodate a "title", "price" and "pages" element.

L E A R N C 675.50 325
C P O I N T E R S 175 225
C P E A R L S 250 250

Declaring a Struct Array

You can also declare an empty struct array. Afterwards, you can either read the data in it with scanf() statements or assign value to each element as shown below −

struct book b[3];
strcpy(b[0].title, "Learn C");
b[0].price = 650.50;
b[0].pages=325;

strcpy(b[1].title, "C Pointers");
b[1].price = 175;
b[1].pages=225;

strcpy(b[2].title, "C Pearls");
b[2].price = 250;250
b[2].pages=325;

Reading a Struct Array

We can also accept data from the user to fill the array.

Example 1

In the following code, a for loop is used to accept inputs for the "title", "price" and "pages" elements of each struct element of the array.

#include <stdio.h>

struct book{
   char title[10];
   double price;
   int pages;
};

int main(){

   struct book b[3];
   
   strcpy(b[0].title, "Learn C");
   b[0].price = 650.50;
   b[0].pages = 325;
   
   strcpy(b[1].title, "C Pointers");
   b[1].price = 175;
   b[1].pages = 225;
   
   strcpy(b[2].title, "C Pearls");
   b[2].price = 250;
   b[2].pages = 325;
   
   printf("\nList of Books:\n");
   for (int i = 0; i < 3; i++){
      printf("Title: %s \tPrice: %7.2lf \tPages: %d\n", b[i].title, b[i].price, b[i].pages);
   }
   
   return 0;
}

Output

When you run this code, it will produce the following output −

List of Books:
Title: Learn C    Price: 650.50 Pages: 325
Title: C Pointers Price: 175.00 Pages: 225
Title: C Pearls   Price: 250.00 Pages: 325

Example 2

In this example, a struct type called student is defined. Its elements are "name"; marks in physics, chemistry and maths; and the "percentage".

An array of three struct student types is declared and the first four elements are populated by user input, with a for loop. Inside the loop itself, the "percent" element of each subscript is computed.

Finally, an array of students with their names, marks and percentage is printed to show the marklist.

#include <stdio.h>

struct student{
   char name[10];
   int physics, chemistry, maths;
   double percent;
};

int main(){

   struct student s[3];
   
   strcpy(s[0].name, "Ravi");
   s[0].physics = 50;
   s[0].chemistry = 60;
   s[0].maths = 70;

   strcpy(s[1].name, "Kiran");
   s[1].physics = 55;
   s[1].chemistry = 66;
   s[1].maths = 77;

   strcpy(s[2].name, "Anil");
   s[2].physics = 45;
   s[2].chemistry = 55;
   s[2].maths = 65;

   for (int i = 0; i < 3; i++){
      s[i].percent = (double)(s[i].physics + s[i].maths + s[i].chemistry)/3;
   }
   
   printf("\nName\tPhysics\tChemistry\t\Maths\tPercent\n");
   
   for(int i = 0; i < 3; i++){
      printf("%s \t\t%d \t\t%d \t\t%d \t\t%5.2lf\n", s[i].name, s[i].physics, s[i].chemistry, s[i].maths, s[i].percent);
   }
   
   return 0;
}

Output

When you run this code, it will produce the following output −

Name  Physics Chemistry Maths Percent
Ravi  50      60        70    60.00
Kiran 55      66        77    66.00
Anil  45      55        65    55.00

Sorting a Struct Array

Let us take another example of struct array. Here, we will have the array of "book" struct type sorted in ascending order of the price by implementing bubble sort technique.

Note: The elements of one struct variable can be directly assigned to another struct variable by using the assignment operator.

Example

Take a look at the example −

#include <stdio.h>

struct book{
   char title[15];
   double price;
   int pages;
};

int main(){

   struct book b[3] = {
      {"Learn C", 650.50, 325}, 
      {"C Pointers", 175, 225}, 
      {"C Pearls", 250, 250}
   };
   
   int i, j;
   struct book temp;
   
   for(i = 0; i < 2; i++){
      for(j = i; j < 3; j++){
         if (b[i].price > b[j].price){
            temp = b[i];
            b[i] = b[j];
            b[j] = temp;
         }
      }
   }

   printf("\nList of Books in Ascending Order of Price:\n");

   for (i = 0; i < 3; i++){
      printf("Title: %s \tPrice: %7.2lf \tPages: %d\n", b[i].title, b[i].price, b[i].pages);
   }
   
   return 0;

}

Output

When you run this code, it will produce the following output −

List of Books in Ascending Order of Price:
Title: C Pointers Price: 175.00 Pages: 225
Title: C Pearls   Price: 250.00 Pages: 250
Title: Learn C    Price: 650.50 Pages: 325

Declaring a Pointer to a Struct Array

We can also declare a pointer to a struct array. C uses the indirection operator (→) to access the internal elements of struct variables.

Example

The following example shows how you can declare a pointer to a struct array −

#include <stdio.h>

struct book {
   char title[15];
   double price;
   int pages;
};

int main(){

   struct book b[3] = {
      {"Learn C", 650.50, 325}, 
      {"C Pointers", 175, 225}, 
      {"C Pearls", 250, 250}
   };

   struct book *ptr = b;

   for(int i = 0; i < 3; i++){
      printf("Title: %s \tPrice: %7.2lf \tPages: %d\n", ptr -> title, ptr -> price, ptr -> pages);
      ptr++;
   }
   
   return 0;
}

Output

When you run this code, it will produce the following output −

Title: Learn C    Price: 650.50 Pages: 325
Title: C Pointers Price: 175.00 Pages: 225
Title: C Pearls   Price: 250.00 Pages: 250
Advertisements