Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!watmath!clyde!burl!ulysses!allegra!mit-eddie!genrad!panda!talcott!harvard!seismo!brl-tgr!gwyn From: gwyn@brl-tgr.ARPA (Doug Gwyn ) Newsgroups: net.lang.c Subject: RE: type cast in initializer Message-ID: <2137@brl-tgr.ARPA> Date: Sat, 1-Feb-86 18:41:20 EST Article-I.D.: brl-tgr.2137 Posted: Sat Feb 1 18:41:20 1986 Date-Received: Mon, 3-Feb-86 05:27:51 EST References: <2124@brl-tgr.ARPA> Organization: Ballistic Research Lab Lines: 32 > > I've found the two lines > > > > int x = 0; > > char *ptr = (char *) &x; > > > > acceptable to every C compiler I've used, until running into the > > Whitesmith's C compiler. They refuse to accept the type coercion "(char *)" > > on the second initialization. As I read K&R this should be acceptable > > through the rules > > The posting did not indicate whether the variables are static or auto. The variables cannot be static, with those declarations. I assume you mean, file scope vs. block scope. In any case, it doesn't matter; the example is valid C code. (However, I wonder what it was trying to do. Treating the address of an int as a (char *) is rarely required, with fread/fwrite buffer parameter being the only common occurrence.) > The initializer should be legal if x is static, but not neccesarily if > x is declared as an auto. If x is auto, the compiler could place it in > a register whose address cannot be taken. That would be a compiler bug. Also, the presence or absence of the type cast would surely be irrelevant in such a case. > The compiler should be smart enough to notice the address operator and > disallow register allocation. K&R only states that variables declared > register cannot have their address taken. Anyone know what ANSI says? "The operand of the unary & operator must be a function locator or an lvalue that designates an object other than a bit-field or an object declared with the `register' storage-class specifier."