• C Programming Video Tutorials

C - Arrays of Structures



In C, 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 struct 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 in the beginning of the code, so that its type can be used inside any of the functions. You can declare an array of structure and later on fill data in it, or you can initialize it at the time of declaration itself.

Initialize struct array

Let use define a struct type named as 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 the memory? Since we have an array of three elements, of struct whose size is 32 bytes, the array occupies 32X3 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

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

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

Read struct array

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

Example

#include <stdio.h>
#include <string.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 Price: %7.2lf No of Pages: %d\n", b[i].title, b[i].price, b[i].pages);
   }
   return 0;
}

Output

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

Example

In the example below, a struct type call student is defined. Its elements are name, marks in phy, che and maths; and the percentage. An array of three struct student types is declared and 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>
#include <string.h>
struct student{
   char name[10];
   int physics, chem, math;
   double percent;
};
int main (){
   struct student s[3];
   strcpy(s[0].name, " Ravi ");
   s[0].physics = 50;
   s[0].chem = 60;
   s[0].math =70;

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

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

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

Output

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

Sort struct array

Let us take another example of struct array. The assay of book struct type is sorted in ascending order of the price by implementing bubble sort technique. Note that elements of one struct variable can be directly assigned to other struct variable directly with assignment operator.

Example

#include <stdio.h>
#include <string.h>
struct book{
   char title[10];
   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 Price: %7.2lf No of Pages: %d\n", b[i].title, b[i].price, b[i].pages);
   }
   return 0;
}

Output

List of books in ascending order of price
Title: C Pointers Price:  175.00 No of Pages: 225
Title: C Pearls Price:  250.00 No of Pages: 250
Title: Learn C Price:  650.50 No of Pages: 325

Pointer to struct array

We can also declare a pointer to struct array. C uses −> operator as the indirection operator to access internal elements of the struct variables.

Example

#include <stdio.h>
#include <string.h>
struct book{
   char title[10];
   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;
   int i;
   for (i=0; i<3; i++){
      printf("Title: %s Price: %7.2lf No of Pages: %d\n", ptr->title, ptr->price, ptr->pages);
      ptr++;
   }
   return 0;
}

Output

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