Mail Archives: djgpp/2000/04/29/11:43:47
J-P wrote:
>Ah. This is the relevant bit from K&R. Appendix A7.3 - Postfix
>Expressions (subsequently "PE"s).
>
> A7.3.1 Array References
>
> A PE followed by an E in square brackets { i.e. PE[E] - jps } is a
> postfix expression denoting a subscripted array reference. One of
> the two expressions must have type "pointer to T", where T is some
> type, and the other must have integral type.
>
>So in my example, "arrayname" /is/ of type "pointer to char". Isn't it?
>According to K&R, at least, there seems no ambiguity.
Well, arrayname will be interpreted as a pointer to char in most
contexts, but really it's something different. One context where it
is clearly different is when you try to take its address. Another is
when you apply sizeof to it, getting the size of the array (80 in your
example), rather than the size of a pointer (4 in DJGPP).
In my original reply to you, I referred to this interpretation as
"conversion", but actually that's too strong strong a word, since it
really is considered to _be_ a pointer in most contexts.
I don't think K&R are at all clear about this, and the piece you quote
above is, at the very least, misleading.
>Perhaps the printf family of functions is clever and recognises pointer to
>pointer, and the standard libraries allow gcc to compile equivalent
>executables. Any idea? Have I interpreted K&R wrong, do K&R slip up at
>this point, or is gcc/printf doing something wiley?
This is not special to printf, nor to gcc. K&R is simply unclear about
this stuff. Whenever arrayname is the name of an array, then
&arrayname == arrayname
will evaluate to 1. (The compiler will, however, give a warning, because
one side is a pointer to an array, while the other side is [interpreted as]
a pointer to a whatever-it's-an-array-of.) I agree that this is horrible,
and I've never felt the need to use &arrayname, but this is certainly the
way it works.
One thing you might like to look at in K&R is in section 5.11 where it
says (talking about functions passed as parameters):
Since they are known to be functions, the & operator is not
necessary, in the same way that it is not needed before an
array name.
This at least hints at the truth, although again it's misleading.
>If anyone wants to suggest a more relevant newsgroup, I'd happily follow
>this there, trn willing.
I suppose this ought to go in a C language newsgroup, but I dislike the
idea of threads disappearing off into a different newsgroup.
S.
- Raw text -