When is a cast not a cast?

Blair P. Houghton bph at buengc.BU.EDU
Fri May 5 05:11:24 AEST 1989


In article <1556 at auspex.auspex.com> guy at auspex.auspex.com (Guy Harris) writes:
>
>>but I find it more than unsettling that a pointer offset can not be
>>expressed and manipulated directly in the pointer type.
>
>Why?  "pointer offsets", if by that you mean "the value of one pointer
>relative to another", aren't the same sorts of objects as pointers.

viz,

	char array[100];
	char *a1, *a2;

	struct ohmigoditsgross brray[100];
	struct ohmigoditsgross *b1, *b2;

	int diffa;

	/* ...code in here setting a1, a2, b1, b2 to what they
	   really should be, within their respective arrays... */
	
	/* How far apart are they? */

	diffa = a2 - a1;  /* So far so C. */

	/* The others should be aligned. */

	b2 = b1 + diffa;  /* Did I just do that? */

I've been doing things like that for years and thinking I was getting away
with the golden eggs.

What I just did was add something that's _really_ a couple of bytes in
virtual size as though it was hundreds of bytes... if I think of them as
only indices, I'm fine.  It's when I start thinking in terms of physical
byte-slots, no matter how it's scaled, that my logical evaluator blows
a rectifier.

It seems that I can actually put a segment, a board, or an ethernet link
in between elements array[33] and array[34], and I could still get that

	a1 = array + 33;
	a2 = array + 34;

	diffa = a2 - a1;

and always expect diffa is exactly 1, even if the ethernet link separating
elements 33 and 34 of array[] is a million miles long.

I.e., scaling doesn't have to be constant.

This is nasty, but it works, and I'll buy it.

I'll stop trying to add pointers.

				--Blair
				  "Even though I want to."



More information about the Comp.lang.c mailing list