Xref: utzoo comp.sys.ibm.pc:22292 comp.sys.intel:605 Path: utzoo!utgpu!attcan!uunet!iscuva!carlp From: carlp@iscuva.ISCS.COM (Carl Paukstis) Newsgroups: comp.sys.ibm.pc,comp.sys.intel Subject: Re: correct code for pointer subtraction Message-ID: <2237@iscuva.ISCS.COM> Date: 15 Dec 88 21:38:11 GMT References: <597@mks.UUCP> <8377@bloom-beacon.MIT.EDU> <8455@sequent.UUCP> <600@mks.UUCP> Organization: ISC Systems Corporation, Spokane, WA Lines: 37 In article <600@mks.UUCP> egisin@mks.UUCP (Eric Gisin) writes: .>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 */ .>} Of course, the code you posted is NOT legal, since the two pointers in the example *do not* point inside the same object. You have verified that the incorrect code is generated when you IN FACT declare "struct six s[10000]"? If so, it's a bona-fide bug. But if it won't work with your example, the worst conclusion you can directly draw is that your example is "not conformant". -- Carl Paukstis +1 509 927 5600 x5321 |"The right to be heard does not | automatically include the right UUCP: carlp@iscuvc.ISCS.COM | to be taken seriously." ...uunet!iscuva!carlp | - H. H. Humphrey