struct array in c code example
Example 1: c struct array
#include<stdio.h>
#define n 3
struct Body
{
double radius;
double mass;
};
struct Body bodies[n];
int main()
{
for(int a = 0; a < n; a++)
{
bodies[a].mass = 0;
bodies[a].radius = 1.0;
}
return 0;
}
Example 2: how to create and return a struct array in C
fentry *read_fentries(FILE *fp){
fentry *files[MAXFILES];
if ((fread(files, sizeof(fentry), MAXFILES, fp)) == 0) {
fprintf(stderr, "Error: could not read file entries\n");
closefs(fp);
exit(1);
}
return files;
}