Create a Array of Structures - C Structure

C examples for Structure:Structure Value

Introduction

The following code shows how to create an array of structure.

typedef struct employee { 
   char fname[10]; 
   char lname[10]; 
   int id; 
   float salary; 
} emp; 

emp emp1[5]; 

To access individual elements in a structure array.

strcpy(emp1[0].fname, "value");  

Demo Code

#include <stdio.h> 
#include <string.h>  

typedef struct scores { 
   char name[10]; 
   int score; /*from ww w  .j  a v a2  s .co  m*/
} s; 

int main()
{ 
   s highScores[3]; 
   int x; 

   strcpy(highScores[0].name, "A"); 
   highScores[0].score = 4; 
   strcpy(highScores[1].name, "B"); 
   highScores[1].score = 3; 
   strcpy(highScores[2].name, "C"); 
   highScores[2].score = 3; 

   for ( x = 0; x < 3; x++ ) 
     printf("\n%s\t%d\n", highScores[x].name, highScores[x].score); 
}

Result


Related Tutorials