为什么在C中不允许分配给数组?

如果我写:

int some_arr[4];
some_arr = {0, 1, 2, 3};

Then my compiler (in this case, GCC) will complain that I don't have an expression before {. So I need to use a compound literal, fine:

int some_arr[4];
some_arr = (int[]){0, 1, 2, 3};

现在,我们看到不允许将值分配给数组。

什么?

I can "circumvent" this with something like memcpy(some_arr, (int[]){0, 1, 2, 3}, sizeof(int[4])), or by assigning to each element of some_arr one-by-one (or through a loop.) I can't imagine that GCC is incapable of parsing the individual assignments from what I've wrote, so it seems to come down to "the standard said no." So why does the standard say this particular thing is off-limits?

评论