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

前端 未结 7 1980
被撕碎了的回忆
被撕碎了的回忆 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:05

    That is because the kind of array you are using is a so-called static array, ie. the memory for it is on the stack. If you would use dynamic arrays (with pointers) your assignment would be legal (but a memory leak would be possible). This would be a shallow copy.

    See also Static array vs. dynamic array in C++

提交回复
热议问题