Jack Klein wrote:
On 16 Sep 2005 05:10:33 -0700, "Greg" <gr****@pacbell.net> wrote in
comp.lang.c++:
[snip]
It's legal to pass an array in a function call, it's just not possible
No, it is not. It is one of Dennis Ritchie's few unfortunate lapses
in judgment. The name of an array, in all contexts except when used
as the operand of the sizeof operator, is converted to a pointer to
its first element. It is literally impossible to pass a naked array
by value.
for the function called to recover it. In C++ an array passed by value
"decays" into a pointer to its first element by the time the receiver
gets it.
It has nothing to do with "when the receiver gets it". The receiver
receives exactly what the function definition specifies, and when you
write a definition with a parameter of type TYPE array[], you are
actually defining a parameter of type TYPE *array. The conversion
happens in the caller, the array is never passed.
It is unfortunate that this syntax was ever allowed in a function
declaration. It has added to the confusion between pointers and
arrays in C and C++ for more than 30 years. And it leads to erroneous
statements like "it's legal to pass an array in a function call",
which help perpetuate the confusion.
You made me curious.
Is there an observable difference in what you write and what Greg hinted at?
For the sake of this discussion, let us suppose there was a language B++
that is identical to C++ except that it uses Greg's way of passing arrays.
Does there exist a program that behaves differently when interpreted as C++
and when interpreted as B++?
[There is this funny thing that the admissible moves of a knight in chess
can be given by different rules:
(a) a knight jumps as though it moved two fields vertically or horizontally
and then one field in a perpendicular direction.
(b) a knight jumps as though it moved one field vertically or horizontally
and then two fields in a perpendicular direction.
(c) a knight jumps as though it moves one field in a diagonal direction and
then one field in a horizontal or vertical direction so that it will not
end on a field neighboring its initial position.
....
On an 8x8 chess board, these rules are provably equivalent. I just wonder if
you and Greg might conincidentally describe the same thing in different
words, where yours just happen to be closer to the wording of the
standard.]
Best
Kai-Uwe Bux