I’m learning functions/pointers, and having a bit of an issue. What I need to write is a C program with main() and two other functions.
Requirements:
read_funct()must allocate enough memory usingmalloc()to store the data.Function prototype for
read_functmust be:int read_funct(int *data_num, double **data_vals, char *filename)
How it’s meant to work:
main()calls the first function:read_funct()read_num()reads binary data from a file. Two values have to be extracted: the no. of values (first 4 bytes), then the values themselves (8 bytes each, so contained in the next 8*no. of values). These correspond todata_numanddata_vals. They have to be printed, the program then returns tomain().main()performs operations to edit the data from the first file.main()calls the second function:write_funct(), which writes the edited data into a new file.
Where I am:
The first function reads data_num correctly, and reads/prints data_vals. This is all working properly. However, I’m trying to print these in main() to verify that I’m performing operations on the correct data, but I can’t get it working.
Note: I’m not trying to get it working with write_funct() at the moment, just taking it step-by-step.
Here’s my current code for main():
int read_funct(int *data_num, double **data_vals, char *filename);
int main()
{
int data_num;
double **data_vals;
//Reads file using read_funct()
read_funct(&data_num, data_vals, filename);
//Check: print data_num
printf("\nCheck No. of Values: %d\n", data_num);
//Check: print data_vals
for(int i = 0; i<data_num; i++)
{
printf("%0.3lf\t", data_vals[i]);
}
return(0);
}
Here’s read_funct()
int read_funct (int *data_num, double **data_vals, char *filename)
{
FILE * fp = fopen(filename, "rb"); //Opening file
//There's code here to check valid file open
//There's code here to determine size and check valid length
//Proceed with reading data_num if file is large enough
char buffer_n[4];
fread(buffer_n, 4, 1, fp);
int res = buffer_n[0]|(buffer_n[1] << 8)|(buffer_n[2] << 16)|(buffer_n[3] << 24); //Convert endian
data_num = &res; //Passes results back to data_num
printf("Number of Data Values: %d \n", *data_num); //Printing results
//Proceeds with calculating data_vals
data_vals = malloc(8*res); //Allocating memory
fread(data_vals, 8, res, fp);
//Prints data_vals
for(int i=0; i<res; i++)
{
printf("%0.3f\t", data_vals[i]);
}
printf("\nEnd of File Read.\n\n");
fclose(fp);
free(data_vals); //Free allocated memory
return(0);
}
Desired output:
Basically, I want it to print out the values from inside read_file() and then print a check in main(), so the output will be something like:
No. of values: 3 //From printf in read_file()
2 4 6
Check No. of values: 3 //From printf in main()
2 4 6
Where I think I'm going wrong:
Fairly sure that the main issue is that I've messed up my pointers and how I've initialised things in
main(). I've been trying to fix this by myself, but I think I need some more experienced help to figure this out.I know that every
malloc()call must have a subsequentfree(), but I'm worried that by doing so the way that I have, maybe I've made it so that I can't retrieve it inmain(). Does it instead need to have an intermediate buffer to which memory is allocated instead?
Help to get this code working would be very greatly appreciated. Thank you!