Being argument, array a[2] isn't copied when calling function

Hi,
I cannot understand why the following happens, please read to the end.
The code below outputs this:
a[]= 00
a[]= 10
a[]= 10
a[]= 10
a[]= 11
a[]= 11
0.

But I was expecting this:
a[]= 00
a[]= 10
a[]= 10
a[]= 00
a[]= 01
0.

This describes how the process is running in machine:
1. Defining a[2]{0,0}; ii=0; aj=0
2. Calling function func(a,ii,aj) |func({0,0},0,0)|
3. func({0,0},0,0) defining w=0; static aa=0
4. func({0,0},0,0) if(0) returns aa=1
5. func({0,0},0,0) for j=0
6. func({0,0},0,0) for Outputing "00", because a[2]={0,0}, look (1).
7. func({0,0},0,0) for if(!0) | because a[0]=0| returns w+=func(a,ii+1,j) |func({0,0},0+1,0)| and calls func({0,0},1,0)
8. func({0,0},0,0) for if func({0,0},1,0) defining w=0
9. func({0,0},0,0) for if func({1,0},1,0) if(1) returns a[0]=1, because of static aa=1, см 4.
10. func({0,0},0,0) for if func({1,0},1,0) for j=0
11. func({0,0},0,0) for if func({1,0},1,0) for Outputing "10", because of a[2]={1,0}, look row #9
12. func({0,0},0,0) for if func({1,0},1,0) for if(!1) |because a[0]=1|
13. func({0,0},0,0) for if func({1,0},1,0) for j=1
14. func({0,0},0,0) for if func({1,0},1,0) for Outputing "10"
15. func({0,0},0,0) for if func({1,0},1,0) for if(!0) |because a[1]=0|
16. func({0,0},0,0) for if func({1,0},1,0) for if if(1==1) |because ii=1, func({0,0},ii,0)|
17. func({0,0},0,0) for if func({1,0},1,0) for if if return 0
18. func({0,0},0,0) for if w=0 |because func({1,0},1,0) gives 0|
19. func({0,0},0,0) for j=1

And from now, something is happening that I cannot understand:
20. func({0,0},0,0) for Outputing "10"
Why so? If func has itselfs local variables, including a[2]={0,0}.
I was expecting this:
20. func({0,0},0,0) for Outputing "00"

So a[2] array is not local variable. Why it happens?

 12345678910111213141516171819202122232425262728293031323334 #include using namespace std; int func(bool a[],int ii,int aj) { int w=0; static bool aa=0; if(aa){a[aj]=1;}else{aa=1;} for(int j=0;j<2;j++) { cout<<"a[]= "<

But if I define array a[2] as a vector, all goes fine.
 1234567891011121314151617181920212223242526272829303132333435 #include #include using namespace std; int func(vector a,int ii,int aj) { int w=0; static bool aa=0; if(aa){a[aj]=1;}else{aa=1;} for(int j=0;j<2;j++) { cout<<"a[]= "< a(2); for(int i=0;i<2;i++)a[i]=0; int ii=0,aj=0; return func(a,ii,aj); } void main() { cout<
Technically array is a pointer.
line int func(bool a[],int ii,int aj) is equivalent to int func(bool* a,int ii,int aj)
So you actually passing pointer to a.
Technically an array is an array, but it almost always degrades into a pointer because of how weird arrays are. Really, arrays are arrays, they just get treated like pointers because they don't have equal rights yet and they don't know how to protest.

There are ways to preserve the array-ness of an array, but it's not worth it.
Last edited on
Thanks a lot!! I've understood it now
Yes, array is a memory area large enough to store some predefined instances of specific type. And array variable usually can be simplified as a pointer to first array element (it is more than just a pointer in case of multidimensional arrays) and passed as such. operator [] does an implicit pointer aryphmetics and dereference.

Sorry, I have musused word technically. It so close to my native language word with multiple meanings.

And why C++ aloows to do stuff like 2[a] is beyound my understanding...
Last edited on
C++ doesn't aloows to do stuff like 2[a]
o_o is this a compiler extension?
I don't know. I turn on most flags telling compiller to treat standard violation as errors and compiling with -Wall. Still doesn't get a single warning. Originally found this doing something like:
 123 int x = 5; int y[10]; x[y] = ... //copy-paste error.
Topic archived. No new replies allowed.