Xref: utzoo comp.sys.ibm.pc:22733 comp.sys.intel:636 Path: utzoo!attcan!uunet!husc6!rutgers!elbereth.rutgers.edu!ron.rutgers.edu!ron From: ron@ron.rutgers.edu (Ron Natalie) Newsgroups: comp.sys.ibm.pc,comp.sys.intel Subject: Re: correct code for pointer subtraction Keywords: C pointer math DAMN WELL IS A BUG!!!! Message-ID: Date: 4 Jan 89 03:17:28 GMT References: <597@mks.UUCP> <3845@pt.cs.cmu.edu> <18123@santra.UUCP> <142@bms-at.UUCP> <6604@killer.DALLAS.TX.US> <51@rpi.edu> Organization: Rutgers Univ., New Brunswick, N.J. Lines: 16 >Obviously when you do the subtraction ( a-b ), the compiler only knows their >adrresses, not the number of elements they differ.So it HAS to calculate the >diference in BYTES and then divide by sizeof( int ). NO! When I do the subtraction, the compiler is expected to do what is nexecssary to find out what the number of elements between them is. It is not the case that this is always going to be subtract two byte pointers (which they aren't) and divide by sizeof. Consider a machine where int pointers are different than byte pointers. You would certainly not want to use the divide case example. Any compiler that gives the wrong answer for pointer subtraction when two elements less than the maximum representable integer value apart (in the same array) are subtracted has broken pointer subtraction. -Ron