Consider this little code
#include <stdio.h>
#include <stdlib.h>
void* foo1(double bar)
{
double s2 = 0.5;
double s1 = bar/s2;
double toreturn[2] = {s1,s2};
printf("Outside main. Second value: %f\n", toreturn[1]); //this line is commented out for the second output. See below text
return toreturn;
}
int main()
{
void* (*foo)(double) = NULL;
foo = &foo1;
double bar = 2.12;
double* resp = foo(bar);
printf("In main. First value: %f\n", resp[0]);
printf("In main. Second value: %f\n", resp[1]);
return 0;
}
The code outputs
Outside main. Second value: 0.500000
In main. First value: 4.240000
In main. Second value: 0.500000
, which is what I expected. However, if I comment out the the print instruction (the one within the definition of foo1
), I get this output:
In main. First value: 4.240000
In main. Second value: 0.000000
, which makes no sense to me! It seems weird to me that a printf
command can change the values of the variables. Can you explain please explain me this behavior?