Why array assignment operation doesn't exist but structure assignment does in C language?

前端 未结 7 2026
被撕碎了的回忆
被撕碎了的回忆 2021-01-18 01:47
int a[10];
int b[10];

a = b; // illegal

typedef struct {
    int real;
    int imag;
    } complex;

complex c,d;
c = d; //legal

[I realize that

7条回答
  •  春和景丽
    2021-01-18 02:29

    Because C says you can't, it says "A modifiable lvalue is an lvalue that does not have array type, does not have an incomplete type", so an array can't be assigned to.

    Moreover, When you use the name of an array in a value context likea = b; , both the names a and b mean &a[0] and &b[0]. Often referred to as an array "decaying" to a pointer.

    However, arrays are not pointers, so trying to assign an array by using pointers wouldn't make sense.

提交回复
热议问题