I am trying to swap between array and pointer in c++
My code is as the following :
void foo(int* a, int* b);
void main()
{
int *a = NULL;
int b[6]={2,3,5,6};
foo(a,b);
}
void foo(int* a, int b[])
{
int * c;
c=a;
a=b;
b=c;
}
While I return out from the Method nothing changed ,
within the method everything work fin but when the method return nothing change.
my question is:
A) what is my mistake.?
B) How should I fix it.
Your mistake is that you assume arrays are pointers. They are not. They can decay to pointers.
You can’t change
b, but you can changea, by passing it by reference: