Re: Am i just blind? Is "array = array;" allowed?
Goran Pusic <goranp@cse-semaphore.com> wrote:
On Aug 27, 4:02?pm, "Johannes Schaub (litb)" <schaub-johan...@web.de>
wrote:
I really can't find where the Standard forbids the following!
? int a[2] = { 1, 2 };
? int b[2];
? b = a;
I thought somewhere the Standard says that lvalue expressions of array type
are nonmodifiable, but I can't find it!
No idea about what standard might think, but if it did think
something, it would still sound like a silly arbitrary constraint.
What would be the rationale to prevent said assignment?
It's not arbitrary at all, and there is a perfectly good rationale
which can be derived from the definition of arrays.
Arrays effectively act as const pointers (in other words "int* const"),
the only exception being that the sizeof() operator returns the space
taken by the entire array rather than the size of a pointer. (I know this
is simplifying quite a lot, and there are probably other examples where
they don't actually act like const pointers, but you get the idea.)
So you can think of "b = a;" as the attempt to modify 'b' to make it
point to the same place as 'a' is pointing to (because that's what pointer
assignment does). However, 'b' is const, so you can't modify it to point
anywhere else.
Of course one could argue that the language should make an exception
to this general principle in the case where you are assigning an array to
another of the same type and size, and make it instead assign the contents
of the array to the other array. I don't know, however, if the
standardization committee had a reason to not impose such an exception
to the general rule (and we are probably talking about the C standardization
committee here, as this limitation is probably coming from C).