When is a cast not a cast?

mcdonald at uxe.cso.uiuc.edu mcdonald at uxe.cso.uiuc.edu
Tue May 23 23:17:00 AEST 1989


/* Written  1:51 pm  May 21, 1989 by peter at ficc.uu.net in uxe.cso.uiuc.edu:comp.lang.c */
In article <2918 at buengc.BU.EDU>, bph at buengc.BU.EDU (Blair P. Houghton) writes:
> 	POINTERS ARE NOT MEMORY LOCATIONS.  THAT'S A CHILDHOOD
> 	EXAMPLE FOR THE PASCAL-DEPROGRAMMED.  GROW UP, C-LAND 

I'm glad you noticed that. You're the only one who seems determined to
think they are.

If pointers were simply memory locations, pointer addition would be a non-
problem. But they aren't, and it isn't. What do you get when you add
segment "xylophone-buggywhip" offset "3 marshmallows" to segment "xylophone-
buggywhip" offset "7 marshmallows"?

-- 
Peter da Silva, Xenix Support, Ferranti International Controls Corporation.

Business: uunet.uu.net!ficc!peter, peter at ficc.uu.net, +1 713 274 5180.
Personal: ...!texbell!sugar!peter, peter at sugar.hackercorp.com.
/* End of text from uxe.cso.uiuc.edu:comp.lang.c */

I believe that pointers in C can be cast to some suitable integral type,
for example unsigned long. Then you can do what you wish with their value.
Then cast back to the pointer of your choice, and if you are suitably
knowledgeable about the target machine, the effect of your choice should
be achieved.

I simply don't understand all the flamage. There seems to be a legal
way to do what some people want to do. If it works, it works, if
it doesn't work, study some more. (For example, on the machine I
am typing this on, an IBM PS/2 Model 80, using my usual C compiler
(Microway NDPC), pointers are simply 32bit offsets from some
system-supplied origin. It is not hard to figure out what will happen
if you cast a few to unsigned int, add and subtract, and cast back.
I can easily figure out whether such an operation would give a useful
result or an illegal memory trap. Such operations are simply target-
machine dependent.)

Could somebody (by e-mail, not posting) explain why there is so much
flamage on this. Can't it simply be dismissed by the one liner
"machine and compiler dependent"? I'll agree that there is no
need to make POINTER ADDITION be legal C, since casts allow the
effect to be achieved. There are certainly flammable subjects
involving pointers, but this isn't one.


Doug McDonald



More information about the Comp.lang.c mailing list