Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utcs!mnetor!seismo!lll-crg!topaz!ll-xn!cit-vax!elroy!smeagol!usc-oberon!sdcrdcf!psivax!friesen From: friesen@psivax.UUCP Newsgroups: net.lang.c Subject: Re: Failure of "long x(cp) char * cp; { return *((long *) cp); }" is a bug? Message-ID: <1329@psivax.UUCP> Date: Wed, 9-Jul-86 18:47:52 EDT Article-I.D.: psivax.1329 Posted: Wed Jul 9 18:47:52 1986 Date-Received: Sat, 12-Jul-86 10:29:09 EDT References: <6152@elsie.UUCP> <3783@reed.UUCP> Reply-To: friesen@psivax.UUCP (Stanley Friesen) Organization: Pacesetter Systems Inc., Sylmar, CA Lines: 35 In article <3783@reed.UUCP> kab@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: ??