So I thought a while back I had away of returning an array from a function by returning a pointer and then using *(ptr + n) where n is 1, 2, 3 etc. to get the elements out. I seem to recall this working but at the moment it isn't, and I can't figure out why it does this. Here is the code I have:
Code:
double *final_ptr = box(data, width, range);
printf("final pointer = %f\n", *final_ptr);
double final_array[5] = { 0 };
final_array[0] = *final_ptr;
printf("final array : %f\n", final_array[0]);
Which was mostly some test code I put in to try to pin down why this wasn't working. The weird thing is it prints:
final pointer = 1.5 (what I wanted)
final array : 0
I do not understand why. But this doesn't happen if you write a = 6; b = a, and then print a and b. So I imagine that it is either something to do with using a pointer or else something to do with the function "box" I have written. But the "box" function returns the result I want, and that seems to carry over to the main function. It is just when I want to put it into an array it does not seem to work. Is there some rule that I am unaware of here?
Any help is much appreciated, thanks.
Edit: I have also just tried putting just a standard double and making it equal to *final_ptr. This also then prints 0. So it seems that, apart from the actual pointer, everything thinks that it is pointing to 0. As you can tell I'm not exactly an expert in C but this seems really weird to me.