I have a binary file which contains N numbers from an array. I want to return a pointer to the elements in the array.
float *** reading(char* read_from) {
float x[n], *p;
p=x;
int i;
FILE *fh= fopen (read_from, "rb");
for (i=0;i<2*n*n;i++)
fread (&x[i], sizeof (x[i]), 1, fh);
fclose (fh);
return p;
}
int main()
{
float *var;
char *file="input.bin"; //this would be user input
var=reading(file);
printf("%f",*var);
}
If I'm running this, the content of var
gets deleted after I handle it. I tried switching to another pointer (declaring a var1
) and it is not working.
Edit: The function header is mandatory. It can not be modified.