🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #6118

arr, &arr (was Bounds checks and a new question)

#6118
From: Chris Torek
Date: Sat, 16 Dec 1989 20:18
38 lines
2105 bytes
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
Technical Details
Message-ID: <21321@mimsy.umd.edu>
Path: arc1-ca.pugleaf.net!utzoo!attcan!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!samsung!uakari.primate.wisc.edu!ark1!nems!mimsy!chris
References: <448@longway.TIC.COM> <450@longway.TIC.COM> <547@mwtech.UUCP>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!attcan!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!samsung!uakari.primate.wisc.edu!ark1!nems!mimsy!chris
From: chris@mimsy.umd.edu (Chris Torek)
Newsgroups: comp.std.c
Subject: arr, &arr (was Bounds checks and a new question)
Message-ID: <21321@mimsy.umd.edu>
Date: 16 Dec 89 20:18:40 GMT
References: <448@longway.TIC.COM> <450@longway.TIC.COM> <547@mwtech.UUCP>
Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742
Lines: 38