-2

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);

}
Jxh
  • 11
  • 5
  • 2
    ``s`` is an array, not a pointer. So when you write ``struct Person s[STUDENT];`` you already allocated memory for array. If you want to allocate dynamically, you need to declare ``s`` as pointer ``struct Person* s;`` – Nikolay K Feb 13 '15 at 02:39
  • Thanks for the help, I have figure out the problem, and it compiled smoothly! – Jxh Feb 13 '15 at 03:46
  • @Jxh You shouldn't edit the problematic code out of your question or it will be harder for others to learn from your mistake. – tab Feb 13 '15 at 07:49
  • [Please don't cast the result of `malloc()`](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc/605858#605858). Also please refrain from editing in changes that invalidate answers, or if it's really necessary, state it clearly in the question itself. – Quentin Feb 13 '15 at 10:46
  • Now, that you've edited your question it's no longer valid. – zoska Feb 13 '15 at 13:43
  • I have changed it back to the original error code for others to learn from my mistakes. Thanks for the advice. – Jxh Feb 15 '15 at 14:25

1 Answers1

1

This

s = (struct Person *)malloc(sizeof(struct Person) * n);

is not only wrong, but unnecessary, s is already an array of struct Person so you cannot assign to it, and you don't need to malloc() space for it.

Maybe you need this

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define STUDENT 3

struct Person {
    int     mass, height;
    int     bmi;
    int     count, num;
    char    name[99];
};

typedef struct Person Person;

int main()
{
    int            i;
    int            n;
    struct Person *s;
    FILE          *in;
    FILE          *out;

    in = fopen("data.txt", "r");
    if (in == NULL) {
        printf("failed to open file!\n");
        exit(1);
    }
    out = fopen("bmi.txt", "w");
    if (out == NULL) {
        printf("failed to open file!\n");
        fclose(in);
        exit(1);
    }
    n = 3;
    s = malloc(sizeof(struct Person) * n);
    if (s == NULL) {
        printf("failed to open file!\n");
        fclose(in);
        fclose(out);
        exit(1);
    }

    for (i = 0; i < n; i++) {
        fscanf("%s", s[i].name);
        fscanf("%d", &s[i].height);
        fscanf("%d", &s[i].mass);

        bmi = mass / (pow(height, 2));

        fprintf(out, "%s%d\n", name, bmi);
    }
    free(s);
    fclose(in);
    fclose(out);
}

and there is no need to cast from void * to any other pointer type, so don't cast malloc().

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97