limitations of casts, pointer and function declarartions...

Steve Glaser steveg at hammer.UUCP
Sat Nov 3 03:28:02 AEST 1984


In article <mcvax.6126> guido at mcvax.UUCP (Guido van Rossum) writes:
>In article <120 at harvard.ARPA> breuel at harvard.ARPA (Thomas M. Breuel) writes:
>>	int x;
>>	char *y;
>>/*### [cc] illegal lhs of assignment operator = %%%*/
>>	(char *)x = y;
>
>Sorry, you're thinking Algol-68.  What you need is:
>	*( (char*) &x ) = y;
>
Mostly right except that what I think Tom wanted was more like:
	*( (char**) &x ) = y;
The same arguments you made still hold.

Note that this is highly unportable code since it assumes that the bit
layout in memory is compatable between ints and char *.  There is
nothing in the language that requires this.  The only requirement in C
is that converting a pointer to a suitably long int and back must not
loose information.

>From what I've seen on net.lang.c, I think the Prime compiler may have
this problem (48 bit pointers, 32 bit ints, when converting a pointer
to an int the ring number portion of the pointer gets droped, when
going the other way the ring number gets set to "user ring", works fine
until you try to use it in non-user-ring code).

Thus, the most portable way to write this expression is still:

	x = (int)y;

	Steve Glaser
	tektronix!steveg



More information about the Comp.lang.c mailing list