Allocating an array in a subroutine

J

JhacK

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)?

Thanks in advance,
J.
 
O

osmium

JhacK said:
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;

That doesn't allocate an array. It declares a pointer to a float, the
pointer is pointing nowhere nice.

The first change:

float bar[10];

Proceed from there.
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)?

Thanks in advance,
J.
 
F

Flash Gordon

JhacK wrote, On 20/05/07 12:54:
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?

You want to allocate memory you say, yet where in the above code do you
try to allocate memory? As far as I can see you do not.
> 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)?

What you want to do is covered by the comp.lang.c FAQ, a resource you
should always check before asking questions here. It is located at
http://c-faq.com and you want questions 7.1, 7.9, 4.8, and, in fact,
most of the rest of the FAQ...

Well, you should read all of sections 4, 6 and 7 as a start as they
cover a lot of misconceptions that you have.

Feel free to ask for further clarifications on the FAQs once you have
read them.
 
B

bert

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]);
--
 
S

smanohar

JhacK said:
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?
1. foo ( ) requires **bar than *bar. From main call foo(&bar);
2. In foo( ) there is no memory allocated to *bar and it is not
pointing to any valid memory.
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)?
within foo( ), you can do memory allocation using malloc ( ) or
declare a static float array in foo ( ) and point *bar to it.
int foo (float **bar)
{
static float farr[10];
*bar = farr;
*bar[0] = 3;
return 1;
}

In case of malloc ( ), I hope you to know what has to be changed in
the above function.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top