[comp.std.c] arr, &arr

chris@mimsy.umd.edu (Chris Torek) (12/17/89)

In article <547@mwtech.UUCP> martin@mwtech.UUCP (Martin Weitzel) writes:
-... I'm teaching courses on C and sometimes I use this little example:

-main()
-{
-	char a[10];
-	printf("%d %d\n", (int)a, (int)&a);
-	printf("%d %d\n", (int)(a+1), (int)(&a+1));
-}

-If the compiler doesn't consider "&a" to be an error (more on this
-soon), running the program will print four numbers. The first two
-are the same, because the first element of an array has the same
-adress as the array in its total, but the third and fourth differ,
-because the diffent type of "a" and "&a":

This is almost guaranteed.  Two problems: addresses might not fit in
`int's, and even if they do, if casting to int retains type information
(e.g., due to type tags), (int)a and (int)&a may produce different
values.

-By definition "a" is a "pointer to char" (the arrays component type),
-but "&a" is a "pointer to an array of 10 char", because for any "v"
-that is declared to have type "T" "&v" has type "pointer to T".

Right.  Many C programmers seem to have a great deal of trouble keeping
this straight.  Two values are not always identical even if they have the
same bit pattern, because they can have different types.  (Rather like
the difference between `int i = 0; float f = 0;', actually.)

-A few days ago I had the chance to try this with Turbo-C, the example
-compiled there without error, but the behaviour was *different*
-depending on "a" beeing static or auto!!

Sounds like a bug.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@cs.umd.edu	Path:	uunet!mimsy!chris