Xref: utzoo comp.sys.ibm.pc:22158 comp.sys.intel:596 Path: utzoo!utgpu!watmath!mks!egisin From: egisin@mks.UUCP (Eric Gisin) Newsgroups: comp.sys.ibm.pc,comp.sys.intel Subject: Re: correct code for pointer subtraction Summary: please read my code Message-ID: <600@mks.UUCP> Date: 13 Dec 88 17:24:33 GMT References: <597@mks.UUCP> <8377@bloom-beacon.MIT.EDU> <8455@sequent.UUCP> Organization: Mortice Kern Systems, Waterloo, Ont. Lines: 24 In article <8455@sequent.UUCP>, sch@sequent.UUCP (Steve Hemminger) writes: > I believe the ANSI std and K&R say pointer subtraction is only allowed inside > one data structure/array. Since the small/medium/large model only allows > arrays <64K, only a 16bit result needs to be computed. Any code that > depends on subtracting pointers into two totally seperate arrays is > non-portable. Please read my code. I have two pointers to "s", a single object. Declaring "s" with 1 or 10000 makes no difference. And the pointers are seperated by 60000, which is less than 64K. struct six { /* HINT: this struct is 6 bytes long */ int i[3]; }; int diff(struct six far* p, struct six far* q) { return p - q; } main(void) { struct six s[1]; printf("%d\n", diff(s+10000, s)); /* 10000 */ printf("%d\n", diff(s, s+100)); /* -100 */ }