I want to pass a float array from one function to another function
I want to make use of the float array type(here container)

``````void funct1()
{
float container[4]={0.1,0.2,0.3,0.4}

void funct2()         ----(1)
{return container;}

}

void funct3()
{

void funct2();

use container array;

}``````

Here what would be the funct2() like

void funct2(container) ----(1)

void funct2(float container[]) ---(2)

use container array

## All 19 Replies

your examples are kind of screwy but I think I know what you want. There are two ways it could be done -- either as an array or a pointer to the data. The difference between the two is how you want to access the data in func2() -- use the index notation such as `container[0]` or pointer notation, such as `*container` . And just to confuse you a little more the second method can also be referenced just like the first.

``````// prototype the function
void func2(float container[]);

void func1()
{
float container[]={0.1,0.2,0.3,0.4};
func2( container );
}

void func2( float container[] )
{

}``````
``````// prototype the function
void func2(float* container);

void func1()
{
float container[]={0.1,0.2,0.3,0.4};
func2( container );
}

void func2( float* container )
{

}``````

In C, an array variable is a pointer to the first element of the array. Hence, you can treat the variable as any other, keeping in mind that manipulating the array variable is different than manipulating the array elements.

``````float *reverse_it( float *a, int len ) {
float x, *b, *c;
for (b = a, c = a+len-1; b < c; ++b, --c) {
x  = *b;
*b = *c;
*c =  x;
}
return a;
}

void print_it( float a[], int len ) {
int i;
for (i = 0; i < len; ++i)
printf( "%f\n", a[ i ] );
}

int main() {
float container[ 4 ] = { 0.1, 0.2, 0.3, 0.4 };

print_it( reverse_it( container, 4 ), 4 );

return 0;
}``````

Notice how `float *a` and `float a[]` are exactly the same thing? The reverse_it function manipulates the array by directly using pointers to the elements of the array. The print_it accesses the array elements by subscripting the array variable. Saying `a[ i ]` is exactly the same as `*(a+i)` .

The other thing to note is that reversing the array actually changes the array in main. That's because container is a pointer to the elements of the array, not the elements themselves. So even though you may have multiple copies of the pointer lying around (two separate variables named a and also b and c) there is ever only one copy of the array itself.

I hope this makes sense.

[EDIT] Alas, too slow...

>>Notice how float *a and float a[] are exactly the same thing
You are right in the code you posted, but the two do have a couple differences. a[] can not use pointer arithmetic like you do with *a so they are not interchangeable.

> a[] can not use pointer arithmetic like you do with *a so they are not interchangeable.
In the context of being a parameter to a function, they are equivalent.

``````void foo ( int a[] ) {
a++;
}
void bar ( int *a ) {
a++;
}``````

You certainly can't do 'a++' if 'a' is an in-scope array. But once you pass 'a' as a parameter to a function, then all you have is a pointer, and the full range of pointer operations become available. Pointer notation or array notation can be used with either form.

Thanks Ancient Dragon and all for posting, I am following your technique
In my code it is showing this error(at the bottom)
TO explain my first code, I have three functions and I am accessing one from within the other

``````float eval_container[4];
void getMove(float eval_container1[]);

function1()
{
eval_container[4]={1.0,2.0,3.0,4.0}
float eval_container1[4]={eval_container[0],eval_container[1],eval_container[2],eval_container[3]};

getMove(eval_container1);
}

function2()
{
void getMove(float eval_container1[]);        --*

printf("Printing value of eval container\n");
for(i=0;i<4;i++)
printf("%f ",eval_container1[i]);
}``````

it shows error in the line here *

In function `calculating_RPR':
(.text+0x1893): undefined reference to `getMove'
collect2: ld returned 1 exit status

Btw this is what I am running

gcc -I/usr/local/include -c container.c
gcc -static container.o -lgsl -lgslcblas -lm

the first one doenst have problem
in the second line the error shows

ok I figured what my problem is

I didnt do the function definition and just did the declaration for getMove()

so this is my latest effort

``````float eval_container[4];
void getMove(float eval_container1[]);

function1()
{
eval_container[4]={1.0,2.0,3.0,4.0}
float eval_container1[4]={eval_container[0],eval_container[1],eval_container[2],eval_container[3]};

getMove(eval_container1);
}

function2()
{

void getMove(float eval_container1[])        --*
{
for(i=0;i<4;i++){eval_container[i]=eval_container1[i];}
}
// getMove();

printf("Printing value of eval container\n");
for(i=0;i<4;i++)
printf("%f ",eval_container[i]);
}``````

the same error shows at *

For some reason, your declaration of getmove() is in the middle of function2()

the same error shows at *

count braces -- functions can not be nested.

THanks for clearing that out Ancient Dragon

I got my basics really wrong here

I just wanted to know one thing, the reason I wrote all my code in that way is because eval_container[] is inside a really big function(100+ line), I cant have the function1() return this value from it as its retun type.

Is there anyother way I can pass this array from one function to another ?
from function1() to function2()
(both these functions are really big :<)

I understand that I have to have the function definition outside both the function, but how will it help in passing the array ?

I am working with Duoas code and trying to find if something similar is possible for me. This is the code

``````float eval_container[4];

function1()
{
eval_container[4]={1.0,2.0,3.0,4.0};
getMove(container);
}

float *getMove(float *a)
{
return a;
}

void print_it(float a[]) {
int i;
for (i = 0; i < 4; ++i)
printf( "%f\n", a[ i ] );
}

function2()
{
print_it(getMove(container)); <--- this is printing all zero's *
}``````

* this is where I am lost, how do I get the array ?

sorry the code should be

``````float eval_container[4];

function1()
{
eval_container[4]={1.0,2.0,3.0,4.0};
getMove(eval_container);
}

float *getMove(float *a)
{
return a;
}

void print_it(float a[]) {
int i;
for (i = 0; i < 4; ++i)
printf( "%f\n", a[ i ] );
}

function2()
{
print_it(getMove(eval_container)); <--- this is printing all zero's *
}``````

Sorry for posting again

Here is the entire short-code

``````#include <stdio.h>

float    eval_container1[4];

float *getMove(float *a)
{
return a;
}

void print_it(float a[]) {
int i;
for (i = 0; i < 4; ++i)
printf( "%f\n", a[ i ] );
}

void funct1(void)
{
float eval_container[4]={1.0,2.0,3.0,4.0};
float eval_container1[]={eval_container[0],eval_container[1],eval_container[2],eval_container[3]};

getMove(eval_container1);
}

main()
{
print_it(getMove(eval_container1));
}``````

funct1 isn't called, so all you're getting is the empty global array.

Thanks Salem for pointing that out

how do i get funct() local value eva_container inside main ?

``````main()
{
funct();
print_it(getMove(eval_container1));
}``````

Perhaps funct() should also return a result, which is then passed to print_it().

If I understand you correctly, you want to define the array and fill it with values in, say, funct1(), then use it elsewhere in, say, main().

You can't do that. The array is local to the function in which it is declared. Hence, it only exists while that function is actually executing. Once the function terminates, the array disappears, and all the pointers you have pointing at it become useless.

The only ways around it is to either define it as a global array (recommended), or use malloc() to create it (meaning you'll also have to free() it later). I don't recommend the second method because all it does is the same as a simple global but with all the grief of memory management and circular thinking --and you are still struggling with the basics. So stick with what is simple.

In general though, you should declare data before you use it, in the function in which you are going to use it. Hence:

``````int main() {
float container[] = { ... };
int i;

do_something_to_container( container, length_of_container );

for (i = 0; i < length_of_container; i++)
cout << container[ i ];

...``````

(Please note that this is pseudo-code. For example, I never defined length_of_container or do_something_to_container(). You are expected to replace these with the correct things.)

Hope this helps.

There is a third option: declare eval_container in main() and pass it around to the other functions.

Er, yes. Both my first post and the entire second half of my second post were dedicated to that option, which I also feel is best.

However, if he is unwilling to do that, a global is the next best thing, especially considering his apparent skill level. When he learns more he'll be doling out advice like us.

And, er, mank, you need to give things better names than "container" and "func1" and the like. If you don't know what something should be named then you likely have only a vague idea of what it should be doing... which I think is part of your difficulty.

Hope this helps.

thanks guys, looks like I need to change my code a lot :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.