arr, &arr (was Bounds checks and a new question)
Chris Torek
chris at mimsy.umd.edu
Sun Dec 17 07:18:40 AEST 1989
In article <547 at mwtech.UUCP> martin at 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 at cs.umd.edu Path: uunet!mimsy!chris
More information about the Comp.std.c
mailing list