相关疑难解决方法(0)

为什么我不能将数组分配为&a =&b?

我有一个问题,分配如下数组:

int a[];
int b[] = {1,2,3};
&a = &b;
Run Code Online (Sandbox Code Playgroud)

我知道我可以使用指针,但我想这样试试......

c

7
推荐指数
2
解决办法
2万
查看次数

数组分配

让我用一个例子来解释 -

#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(..) …

c++ arrays

4
推荐指数
1
解决办法
1万
查看次数

标签 统计

arrays ×1

c ×1

c++ ×1