foo = *((bar *)stuff)

Yair Zadik yair at tybalt.caltech.edu
Sat Apr 29 18:37:29 AEST 1989


In article <712 at tekno.chalmers.se> d83_sven_a at tekno.chalmers.se (Sven (Sciz) Axelsson) writes:
>Am I really out of line, or is this a true-to-God bug in the final release
>of MPW C 3.0?? Isn't this code supposed to assign the integer value contained
>at memory position 10 to the variable x?
>
>	x = *((int *)10);
>
>In MPW C 3.0 this generates exactly the same code as would x = 10.
>Am I right or am I right?
>
>	Sven Axelsson		d83_sven_a at tekno.chalmers.se

Sounds like a bug to me.  Correct me if I'm wrong, but (int *) 10 should
convert the integer 10 to a pointer, then the * should dereference the
pointer, which on most (*not* all) machines should read location 10 of
memory.  Note that this is extremely nonportable and very machine (even
compiler) dependent.  How an integer is translated into a pointer is not
defined (unless the integer is 0 which is translated to a null pointer) and
may vary depending on the compiler, although it usually (again *not* always)
stays the same on a single machine if there is a single obvious translation.
On 80x86 machines this can cause problems since 'far' pointers may be 
different from 'near' pointers, and pointers can be dependent on segment
registers. USE WITH CAUTION.

yair at tybalt.caltech.edu
yair at romeo.caltech.edu



More information about the Comp.lang.c mailing list