K
Kaz Kylheku
Ian Collins said:
A pointer is a reference to the object pointed to. See 3.1.2.5 of
C89 or 6.2.5 of C99.
Perhaps. And perhaps they simply don't bother to call a pointer a
reference because it's more idiomatic to call it a pointer.
To be precise, a pointer /is/ a reference.
Note that this does not mean that passing a pointer equates to pass
by reference. It doesn't. You can pass a reference in C, but you
can't pass /by/ reference!
Why not? If a pointer is a reference, we are passing the object itself by means
of that pointer. Ergo, by means of a reference. By reference, in short. In
English, we can shorten ``using'' or ``by means of'' to just ``by''.
``To die from the consequences of a wound inflicted by means of a sword'' is to
``die by sword''. See? To travel by means of an aircraft is to ``travel by
plane'', or ``fly''.
Now if I pay for something by means of a token carried on a plastic card,
connected to a credit card account of the VISA type, can I not call that
``paying by VISA?''
Or is it the case that I'm not, semantically, in fact paying? I'm just passing
a plastic card that is then returned to me, and signing some meaningless piece
of paper?
Is it the case that you can pass a credit card, but you can't pass payment /by/
credit card?
The way I see it, C pointers provide the means to implement a thorough
emulation of that which is known as pass-by-reference.
Some languages that have pass-by-reference have features in that mechanism
that we don't have in pointers. But are those key, defining features without
which it wouldn't be called pass-by-reference? Or are they refinements? E.g.
we can declare that some reference parameter is an ``out'', so that the
function will not access the value, but only store one. Therefore passing a
reference to an uninitialized variable is okay to an out parameter; and
accessing, inside the function, an out parameter without writing to it first,
is diagnosed. But can pass-by-reference exist without such a refinements?
(Moreover, could we not, in principle, add a qualifier to pointers to express
the same thing?)
Other than that, any pass-by-reference situation in a language that has
pass-by-reference can be expressed with C pointers.
Moreover, this can be done without any change in the level of abstraction, just
with a bit of syntactic sugar, like explicitly taking the address and
dereferencing, which we rather don't mind doing because it documents the shady
manipulation that is going on.
Adding some &'s and *'s doesn't change the level of abstraction.
I.e. what I'm claiming is not all like the argument that ``any behavior you get
with such and such high level feature, I feature X, I can perform in assembly
language''. This is at basically the same level.