473,387 Members | 1,925 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Allocating an array in a subroutine

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.

May 20 '07 #1
4 2150
"JhacK" writes:
>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.

May 20 '07 #2
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.
--
Flash Gordon
May 20 '07 #3
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]);
--

May 20 '07 #4

JhacK wrote:
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.
>
Thanks in advance,
J.
May 25 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Andrew Fleet | last post by:
Hi, I'm looking at returning a reference to an array I create within a subroutine. I could do this... sub foo { my @theArray; <snip>
1
by: Sam | last post by:
Hello all I have a two dimensional array (the dimensions are not known) that needs to be passed to fortran from c++, allocate the dimensions of the array in fortran code, do some filling up of...
6
by: Neo | last post by:
Dear All, I want to know how a subroutine should return an array of values to the main program. From the main program, I call a sub-routine 'get_sql' which then fetches data from oracle db using...
3
by: Erik S. Bartul | last post by:
lets say i want to fill up a multidimentional array, but i wish to allocate memory for it on the fly. i assume i declare, char **a; but how do i allocate memory for the pointers, so i can...
15
by: fix | last post by:
Hi all, I am writing a program using some structs, it is not running and I believe it is because there's some memory leak - the debugger tells me that the code causes the problem is in the malloc...
1
by: Peter Oliphant | last post by:
I'm using VS C++.NET 2005 Express in /clr mode. Say I have a PointF array: array<PointF>^ point_array = gcnew array<PointF>(100) ; Now I want to re-allocate point_array, and in doing so also...
3
by: Stephen Travis | last post by:
I'm trying to write a subroutine that will fill an array of some type with several objects of that type. The subroutine works fine if I directly reference the array but if I pass the array as an...
6
by: Francois Grieu | last post by:
Hello, I'm asking myself all kind of questions on allocating an array of struct with proper alignment. Is the following code oorrect ? I'm most interested by the statement t =...
6
by: johntology | last post by:
Hello, I've made a two dimensional array using references, which I gather is the only way to do it in Perl. I now need to pass each interior array to a subroutine for processing and can't quite...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.