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

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

    a is actually the "pointer" to the first element of array and it's a constant "pointer", so you are trying to assign an l-"pointer".

    you can achieve what are you trying to do by :

    struct arraystruct
    {
      int t[10];
    };
    
    
    struct arraystruct a,b;
    
    a=b;
    

    EDIT:well i forgot to mention that there are a few exceptions where an array should not be considered as a pointer:

    -you can use sizeof(array) but you cannot use sizeof(pointer)

    -array of literal string

    -a and &a are the same

提交回复
热议问题