I understand that the C standard prohibits the use of arrays as modifiable lvalues, that is, on the left-hand side of an assignment:
int lhs[4], rhs[4] = {0, 1, 2, 3};
lhs = rhs; /* illegal! */
Now, I have been wondering why this is the case. I could see the statement above (and any other assignment that writes to an array) being defined equivalent to
memcpy((void *) lhs, (void *) rhs, sizeof(lhs));
and imposing the burden of assuring that rhs
is large enough on the user, but it wasn't decided this should be the case.
However, a very similar example does work perfectly fine:
struct { int a[4]; } lhs, rhs = {{0, 1, 2, 3, 4}};
lhs = rhs;
Just by wrapping the array in a structure, we can obtain exactly the behaviour described above, that is, the assignment lhs = rhs
is equivalent to:
memcpy((void *) &lhs, (void *) &rhs, sizeof(lhs));
What is the rationale for this (as I feel) inconsistency? Is there any problem with allowing array assignments interpreted as memcpy
s?