Failure of "long x(cp) char * cp; { return *((long *) cp); }" is a bug?

friesen at psivax.UUCP friesen at psivax.UUCP
Thu Jul 10 08:47:52 AEST 1986


In article <3783 at reed.UUCP> kab at reed.UUCP (Kent Black) writes:
> 
>Oh, am I in over my head. . .
>
>If a (char *) can reference any `byte' of memory, and a (long *) can
>can only reference, as example, even number word addresses, then to have
>the compiler correctly cast a char * to a long * would mean it rearranged
>your data for you.  In particular, an array of characters would have to
>be remade into an array of longs with the apropriate (high or low) byte
>holding the character.  Is this all wrong?

	Yes, the  purpose of the cast is to allow you to look at your
array of char(or whatever) *as if* it were an array of longs. It would
defeat the purpose of the cast if the compiler rearranged the memory
for you!
>
>But do you *really want* to cast a char pointer to a long pointer just
>to dereference and return the value?  The example seems to instead call
>for dereferencing the char * and casting the value as a long, e.g.,
>		return (long) *cp;
>  
	No, this does something completely different. This reads a
single byte from memory, *extends* it to a long and returns the
result. On the other hand 
		return *(long *)cp;

reads a long from memory and returns it. That is it sort of acts like
a union and allows you to look at the same piece of memory as having
different types.
-- 

				Sarima (Stanley Friesen)

UUCP: {ttidca|ihnp4|sdcrdcf|quad1|nrcvax|bellcore|logico}!psivax!friesen
ARPA: ??



More information about the Comp.lang.c mailing list