On 20 May, 12:54, JhacK <gbocc...@gmail.comwrote:
I need to allocate a float array delegating the task to a subroutine .
The code I wrote is:
int foo(float *bar)
{
bar[0] = 3;
return 1;
}
int main (int argc, char* argv[])
{
float *bar;
int ret;
ret = foo(bar);
printf("%5.5f", bar[0]);
}
Why doesn't it work? How can I modify it, without using the return in
foo to retrieve the array (I want to use the return only to manage
errors)?
You are trying to run before you can walk, I think.
In particular you see much too much in the literal
match between *bar in the declaration of foo, and
*bar at the point where you call it.
The smallest change to make your int main (...) work
is:
float baz;
int ret;
ret = foo(*baz);
printf("%5.5f", baz);
When reading all this, you then say to yourself:
I have declared foo to take a parameter which is the
address of some float.
I have called foo with a parameter which is the
address of a particular float.
You can later be more ambitious in int main(...) with:
float baz[10];
int ret2, ret5;
ret2 = foo(baz+2);
ret5 = foo(baz+5);
printf("%5.5f %5.5f", baz[2], baz[5]);
--