16.4 – Array of struct type

by subbu on September 16, 2014

Array of struct variables:

Any variable belongs to struct type is capable to store the details of a single entity. If we want to handle the details of “n” entities then it is a good idea to declare an array of struct type rather declaring multiple variables because it improves the flexibility in program development.

struct product
{
 int sno;
 char pname[50];
 float price;
 int qty;
 float tot;
};

struct product x;

Here the struct type of variable “x” is capable to store the details of a single product.

struct product x[5];

Here x[5] is an array of “struct product” type where every element of array has all the members of “product” and capable to store the details of a product. Now the total array is capable to store the details of 5 products

Array of struct variables

Details of 5 products can be accessed using a loop

for(i=0;i<5;i++)
{
 scanf("%d",&x[i].pcode);
 scanf("%s",x[i].pname);
 scanf("%f",&x[i].price);
 scanf("%d",&x[i].qty);
 x[i].bill=x[i].price*x[i].qty;
}

For every iteration, details of one product are accessed and stored into struct array. For five iterations details of 5 products would be accessed and stored.

Program:

#include<stdio.h>
struct product
{
 int sno;
 char pname[50];
 float price;
 int qty;
 float tot;
};
int main()
{
 struct product x[50];
 int i,n;
 float bill,temp;
 printf("How many products?");
 scanf("%d",&n);
 printf("Enter the details of %d products:\n",n);
 for(i=0;i<n;i++)
 {
   x[i].sno=i+1;   /* storing sno automatically which is 1 more than index*/
   printf("\nName:");
   scanf("%s",x[i].pname);
   printf("Price:");
   scanf("%f",&temp);
   x[i].price=temp;
   printf("Quantity:");
   scanf("%d",&x[i].qty);
   x[i].tot=x[i].price*x[i].qty;
 }
 printf("\n\n                   XYZ SUPER BAZAR\n");
 printf("-----------------------------------------------------------------\n");
 printf("Sno     Name              Price         Qty       Total \n");
 printf("-----------------------------------------------------------------\n");
 bill=0;
 for(i=0;i<n;i++)
 {
   printf("%d\t%s\t\t%f\t%d\t%f\n",x[i].sno,x[i].pname,x[i].price,x[i].qty,x[i].tot);
   bill=bill+x[i].tot;  /* Adding totals to bill */
 }
 printf("                                                -----------------\n");
 printf("                                                %f\n",bill);
 printf("                                                -----------------\n");
 return 0;
}

Execution:
How many products?3
Enter the details of 3 products:
Name:Burger
Price:85
Quantity:2

Name:Pastha
Price:120
Quantity:1

Name:Pizza
Price:250
Quantity:2

XYZ SUPER BAZAR
—————————————————————–
Sno     Name              Price         Qty       Total
—————————————————————–
1       Burger          85.000000       2       170.000000
2       Pastha          120.000000     1       120.000000
3       Pizza           250.000000      2       500.000000
—————–
790.000000
—————–

Example explained:
Accepted the details of “n” products from keyboard and stored into array of structure variables.
Printed the details of all products purchased including with bill as output.

Initialization of a struct array:

A struct array can be initialized wile declaration like a set of sets of values

struct box x[]={{3,4,5},{6,7,8}};

Here the first set {3,4,5} would be assigned to x[0] and {6,7,8} would be assigned to x[1]

#include<stdio.h>
struct box
{
 int b;
 int w;
 int h;
};
int main()
{
 struct box x[]={{3,4,5},{6,7,8}};
 int i;
 for(i=0;i<2;i++)
 {
  printf("Sides of %d box:",i+1);
  printf("%d\t%d\t%d\n",x[i].b,x[i].w,x[i].h);
 }
 return 0;
}

Output
Sides of 1 box:3        4       5
Sides of 2 box:6        7       8

Even a set of elements can be initialized to struct array but enough elements of suitable sequence that matches with the members must be initialized

struct box x[]={3,4,5,6,7,8};

Program:

#include<stdio.h>
struct box
{
 int b;
 int w;
 int h;
};
int main()
{
 struct box x[]={3,4,5,6,7,8};  /* assigning set of elements */
 int i;
 for(i=0;i<2;i++)
 {
  printf("Sides of %d box:",i+1);
  printf("%d\t%d\t%d\n",x[i].b,x[i].w,x[i].h);
 }
 return 0;
}

Output
Sides of 1 box:3        4       5
Sides of 2 box:6        7       8

If enough elements are not supplied then remaining members would be initialized with 0s

Program:

#include<stdio.h>
struct box
{
 int b;
 int w;
 int h;
};
int main()
{
 struct box x[]={3,4,5,6}; /* Assigning shortage of members */
 int i;
 for(i=0;i<2;i++)
 {
   printf("Sides of %d box:",i+1);
   printf("%d\t%d\t%d\n",x[i].b,x[i].w,x[i].h);
 }
 return 0;
}

Output:
Sides of 1 box:3        4       5
Sides of 2 box:6        0       0

Previous post:

Next post: