我有一个问题,分配如下数组:
int a[];
int b[] = {1,2,3};
&a = &b;
Run Code Online (Sandbox Code Playgroud)
我知道我可以使用指针,但我想这样试试......
让我用一个例子来解释 -
#include <iostream>
void foo( int a[2], int b[2] ) // I understand that, compiler doesn't bother about the
// array index and converts them to int *a, int *b
{
a = b ; // At this point, how ever assignment operation is valid.
}
int main()
{
int a[] = { 1,2 };
int b[] = { 3,4 };
foo( a, b );
a = b; // Why is this invalid here.
return 0;
}
Run Code Online (Sandbox Code Playgroud)
是因为,当传递给函数时,数组衰减到指针,可以进行foo(..) …