I am at a lost here, I don't understand why s = (struct Person *)malloc(sizeof(struct Person) * n);
won't work? This assignment is to find the BMI in the data.txt file. Which only contain this
3
Pikachu 50 37
Godzilla 1000 1000
Holmes 178 67
and output it onto a BMI.txt file. With the requirement for Allocate memory block. Thanks in advance.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define STUDENT 3
struct Person {
float mass, height;
float bmi;
int count, num;
char name[99];
};
typedef struct Person Person;
int main()
{
int i;
FILE *in, *out;
in = fopen("data.txt", "r");
if (in == NULL) {
printf("failed to open file!\n");
exit(1);
}
out = fopen("bmi.txt", "w");
struct Person s[STUDENT];
s = (struct Person *)malloc(sizeof(struct Person) * i);
for (i = 0; i < 3; i++) {
fscanf("%s", &s[i].name);
fscanf("%lf", &s[i].height);
fscanf("%lf", &s[i].weight);
bmi = mass / (pow(height, 2));
fprintf(out, "%s%3.2f\n", name, bmi);
}
free(s);
}
fclose(in);
fclose(out);
}