Write a program to use array of structure.
#include<stdio.h> #include<string.h> typedef struct mobile_info { char brand[20]; int model; float price; }MOBILE_INFO; int main() { int i,TotalArrayElements; MOBILE_INFO v[4]; TotalArrayElements = (sizeof(v)/sizeof(v[0])); for(i = 0;i < TotalArrayElements;i++) { printf("Mobile[%d] Brand = ",i); scanf(" %[^\n]",v[i].brand); printf("Mobile[%d] Model = ",i); scanf("%d",&v[i].model); printf("Mobile[%d] Price = ",i); scanf("%f",&v[i].price); } printf("\nMobile Data\n"); for(i = 0;i < TotalArrayElements;i++) { printf("Mobile[%d] = %s %d %f\n",i,v[i].brand,v[i].model,v[i].price); } return 0; }
Output
Mobile[0] Brand = BlackBerry Mobile[0] Model = 10 Mobile[0] Price = 13999 Mobile[1] Brand = Apple Mobile[1] Model = 5 Mobile[1] Price = 19999 Mobile[2] Brand = Apple Mobile[2] Model = 6 Mobile[2] Price = 32000 Mobile[3] Brand = MI Mobile[3] Model = 5 Mobile[3] Price = 21000 Mobile Data Mobile[0] = BlackBerry 10 13999.000000 Mobile[1] = Apple 5 19999.000000 Mobile[2] = Apple 6 32000.000000 Mobile[3] = MI 5 21000.000000