The designers of the C language decided that it should not be possible to assign arrays by value. At the time this seemed a sensible decision (early 1970s) - memory and processor speed were very limited and they considered that having a = b;
make a
and b
both refer to the same array was something that would be a much more common intent than having a = b;
be used to copy the contents of one array to another.
In fact this was already common usage: in the B programming language (precursor to C), the equivalent of int a[10];
actually meant to allocate both a pointer, and a block of 10 ints, and point the pointer to the block of 10 ints. You could actually make an array "point" somewhere else in B.
C changed the meaning of an array definition that it only allocates the block of ints; and added "The Rule": when you use the array's name in an assignment expression (and most other expressions) the array is implicitly converted to a pointer to the first element. So, if a
is a pointer and b
is an array, then you can still write a = b;
to make a
behave like an alias for b
. Although you can no longer have a = b;
where a
is an array.
In the first ANSI C standard in 1989 they added the ability to copy structs by value (this had existed in some compilers previously but wasn't universal), with the corollary that if the struct contains an array then the array gets copied by value. But it was far too late to go back and change the meaning of a = b;
to copy arrays by value, too much code was written that already depends on The Rule.
Reference: The Development of the C Language - Dennis M. Ritchie
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…