pointers to arrays and the '&' operator
Dave Jones
djones at megatest.UUCP
Fri Feb 17 11:18:34 AEST 1989
>From article <3927 at ingr.com>, by crossgl at ingr.com (Gordon Cross):
>
> Allright, the recent discussion regarding pointers to arrays in C reminds
> me of something that I consider to be a major deficiency of the language.
> I could not find any explicit reference to this in the standard (admittedly
> I have an old copy) but Harbison and Steele do mention that it is not legal.
> Since I am allowed to declare something that has type "pointer to an array
> of...", then why am I not permitted to apply the '&' (address of) operator
> directly to an array?? Yes, before you say it, I know that the array name
> is converted to a pointer in expressions but I also know that the usual
> conversions do not apply to the '&' operator. It seems perfectly reasonable
> to expect that the expression &E where E is an array should result in a
> constant of type "pointer to array"!!
To me it seems imperfectly reasonable. In C, unlike Pascal, there
is nothing you can do with an entire array. I can live with that.
So if there is nothing you can do with an entire array, why do you
want a pointer to one? What are you going to use it for?
> What thoughts do the rest of you have on this???
It's not broke.
> Do more recent versions of the standard address (no pun intended)
> this concern??
>
Why should they?
The declaration "int E[how_many];" allocates any array.
In an expression, "E" evaluates to the lvalue of the first element of
the array. Thus "&E" would evaluate to the lvalue of the lvalue
of the first element of the array, and there ain't no setch thang.
By the way, were you aware of the fact that E[i], E+i and [i]E
all mean the same thing?
...
Another thought. I think "structs" were added to C later, and behave
in a more Pascalish manner: You can pass them, in toto, to functions,
copy one to anyother with an assignment statement, etc.,
and thus you can take the address of one. So if you really want
to sling arrays around, rather than their lvalues, you can do
this:
struct
{
int stuff[how_many];
}E;
But in practice, you almost always want to sling the lvalues of
arrays, not the whole things.
More information about the Comp.lang.c
mailing list