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 )
{
}
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
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? Thereverse_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 inmain. 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...
Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
>>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.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
> 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.
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
For some reason, your declaration of getmove() is in the middle of function2()
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
the same error shows at *
count braces -- functions can not be nested.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
funct1 isn't called, so all you're getting is the empty global array.
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
Perhaps funct() should also return a result, which is then passed to print_it().
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
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 definedlength_of_container or do_something_to_container(). You are expected to replace these with the correct things.)
Hope this helps.
Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
There is a third option: declare eval_container in main() and pass it around to the other functions.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
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.
Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229