Path: utzoo!utgpu!water!watmath!clyde!att!rutgers!cmcl2!nrl-cmf!ames!oliveb!sun!thetone!swilson From: swilson%thetone@Sun.COM (Scott Wilson) Newsgroups: comp.sys.mac.programmer Subject: Pointer Subtraction in THINK C 3.0 Message-ID: <63033@sun.uucp> Date: 5 Aug 88 16:59:36 GMT Sender: news@sun.uucp Reply-To: swilson@sun.UUCP () Organization: Sun Microsystems, Mountain View Lines: 57 I was a little suprised when I ran this program under THINK C 3.0: #include printi(i) int i; { printf("i = %d\n", i); } char buf[10]; _main() /* so I can redirect output */ { char *cp1, *cp2; cp1 = buf; cp2 = buf + 5; printi(cp2 - cp1); printi((int) (cp2 - cp1)); printi(&buf[5] - buf); printi((int) (&buf[5] - buf)); } and got the output: i = 0 i = 5 i = 0 i = 5 It looks as though THINK C 3.0 is using long as the resulting type for pointer subtraction. (A long gets pushed on the stack instead of an int, its upper two bytes are zero so printi prints 0.) According to K&R: If two pointers to objects of the same type are subtracted, the result is converted ... to an int ... However, K&R 2nd ed. (and I assume ANSI) says: If two pointers to objects of the same type are subtracted, ... The type of the result ... is defined as ptrdiff_t in the standard header Which definiton does THINK C 3.0 claim to use? Do they specify it? Is there a stddef.h and is ptrdiff_t defined to be long? I'm sorry if this a RTFM question, but either I'm at work with the C manuals or at home with THINK C manuals. -- Scott Wilson arpa: swilson@sun.com Sun Microsystems uucp: ...!sun!swilson Mt. View, CA