Passing Arrays of Structures - C Structure

C examples for Structure:Structure Value

Introduction

To pass an array of structures, simply supply the function prototype with a pointer to the structure.

Demo Code

#include <stdio.h> 
#include <string.h> 
typedef struct employee { 
  int id; //from  w  w  w.ja  v  a 2  s  . c  o m
  char name[10]; 
  float salary; 
} e; 

void processEmp( e * ); //supply prototype with pointer of structure type 

int main() { 
   e emp1[3] = {0,0,0}; 
   int x; 
   processEmp( emp1 ); //pass array name, which is a pointer 
   for ( x = 0; x < 3; x++ ) {  
      printf("\nID: %d\n", emp1[x].id); 
      printf("Name: %s\n", emp1[x].name); 
      printf("Salary: $%.2f\n\n", emp1[x].salary); 
   }
   return 0;
}

void processEmp( e * emp ) //function receives a pointer 
{ 
   emp[0].id = 123; 
   strcpy(emp[0].name, "Mary"); 
   emp[0].salary = 65000.00; 
   emp[1].id = 234; 
   strcpy(emp[1].name, "Jack"); 
   emp[1].salary = 28000.00; 
   emp[2].id = 456; 
   strcpy(emp[2].name, "Edith"); 
   emp[2].salary = 48000.00; 
}

Related Tutorials