When it is amoral... (Re: When is a cast not a cast?)

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


In article <1558 at auspex.auspex.com> guy at auspex.auspex.com (Guy Harris) writes:
>
>Can you come up with a similar definition for the addition of two
>pointers?

As Gwyn said (and he thinks I rejected it, but I didn't, he should check
out that I was referring to his statement, not mine...) "addition of two
pointers is meaningless."  Addition of 0xfffe and 0xfffe puts you over the
top of a data segment and that's bogus.  Add that, as I posted earlier
today, I now see that the "scaling" of pointers isn't to be trusted,
either, and this addition doesn't even do what it appears to do, whether
that's what I want or not.

Addition of two pointers?  I'd never do it.  Addition of two things that
have pointer type?  That I might do.

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

	float *diffa;

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

	/* ...Code setting the array values, and pointing
	   a1, a2, and b1 into their respective arrays... */

	dobedobedoo(array,brray,&a1,&a2,&b1);
	
	/* Want to align the pointer differences; how far
	   apart are the a's ? */
	
	diffa = a2 - a1;  /* Bogus in normal C */

	/* Now align the b's */

	b2 = b1 + diffa;

What does that do?  It tries to add a float-pointer to a
struct-ugly-pointer.  Mismatch errors fill my screen with a phosphorescent
radiance.  And well they should.

Normal C, however, allows this sort of thing by telling one to declare
	
	int diffa;

and merrily arithmeticise.  No warnings, no complaints, not even a bump
in the great, grassy field of complacen-C....

Mind you, it's real nice to be able to do it this way, and if one
couldn't, we'd all be screaming for it, but it feels the same as adding
shorts, longs, and ints together haphazardly.

				--Blair
				  "Okay, `long diffa'.
				   There, you happy?"



More information about the Comp.lang.c mailing list