[comp.lang.c] pointers to arrays and the '&' operator

crossgl@ingr.com (Gordon Cross) (02/14/89)

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"!!  What thoughts do the rest of you have
on this???  Do more recent versions of the standard address (no pun intended)
this concern??


P.S.  The same argument can be said for pointers to functions and &E where
      E is the name of a function...
-- 

Gordon Cross             UUCP:      uunet!ingr!crossgl     "all opinions are
111 Westminister Way     INTERNET:  crossgl@ingr.com        mine and not those
Madison, AL 35758        MA BELL:   (205) 772-7842          of my employer."

chris@mimsy.UUCP (Chris Torek) (02/17/89)

In article <3927@ingr.com> crossgl@ingr.com (Gordon Cross) writes:
>I could not find any explicit reference to this in the standard (admittedly
>I have an old copy) ...

Either your copy is too old or you did not look hard enoguh:

>why am I not permitted to apply the '&' (address of) operator
>directly to an array?

The pANS allows it.  (PCC can be made to allow it by *removing* a few
statements from the compiler.)
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@mimsy.umd.edu	Path:	uunet!mimsy!chris

gwyn@smoke.BRL.MIL (Doug Gwyn ) (02/17/89)

In article <3927@ingr.com> crossgl@ingr.com (Gordon Cross) writes:
>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.

It is actually the fact that arrays are not first-class objects in C
that is the deficiency.  This cannot be fully remedied without
invalidating large amounts of existing correct code.

>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??

In ANSI C, you are allowed to do so.

Many existing (PCC-based) compilers permit &array but ignore the &.
That is not the ANSI C behavior, though.  For maximal portability
you should avoid using &array for a few years.

ark@alice.UUCP (Andrew Koenig) (02/17/89)

In article <3927@ingr.com>, crossgl@ingr.com (Gordon Cross) writes:

> 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??

You can in ANSI C.
-- 
				--Andrew Koenig
				  ark@europa.att.com

djones@megatest.UUCP (Dave Jones) (02/17/89)

From article <3927@ingr.com>, by crossgl@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.
  

djones@megatest.UUCP (Dave Jones) (02/17/89)

From article <2013@goofy.megatest.UUCP>, by djones@megatest.UUCP (Dave Jones):

>   
>   By the way, were you aware of the fact that E[i], E+i and [i]E
>   all mean the same thing? 
>   

Please ignore the above.  My brain tumor was acting up again.

You know what I meant to say, (don't you?).

guy@auspex.UUCP (Guy Harris) (02/18/89)

>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?? ... Do more recent versions of the standard
>address (no pun intended) this concern??

All the versions of the dpANS that I've seen allow you to apply "&" to
an array; I would be extremely surprised if the latest version of the
pANS didn't permit you to do so as well.  The reason why you're not
generally permitted to do this on, say, UNIX systems is that many C
compilers on UNIX systems don't implement that part of the language in
the dpANS (e.g. the Ritchie compiler, or PCC).

>P.S.  The same argument can be said for pointers to functions and &E where
>      E is the name of a function...

The May 19, 1988 dpANS indicates that "function designators" don't get
turned into "pointer to function" expressions when used as the operand
of "sizeof" or unary "&", so that version (and presumably later
versions) of the (draft) proposed standard allows you to do &E as well.