Dear all,
Suppose i have a program that is consisted of a struct. this struct will be used as an array.
Code:
typedef struct
{
char name[30];
int salary;
} person;
How do i define an flexible array for this struc ? i don't know how many arrays which i need yet.
and how can i calculate the sum of all person salary ?
I wrote the simple code, but i think not solve my problem
Code:
#include <stdio.h>
typedef struct
{
char name[30];
int salary;
} person;
int sumStructs(person*, int);
main(void)
{
person a[10], *pa; /* I still put non flexible array*/
pa = a;
int i = 0;
for(i=0; i< 10; i++)
{
a[i].i = i+1;
}
int d = sizeof(a)/sizeof(person);
int c = sumStructs(pa, d);
printf("sum i adalah %d \n",c);
}
int sumStructs(person*pa, int d)
{
int sum = 0, j= 0;
while(j<d)
{
sum += (*pa).i;
pa++;
j++;
}
return sum;
}