Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!watmath!clyde!rutgers!ames!ucbcad!ucbvax!sdcsvax!sdchem!tps From: tps@sdchem.UUCP Newsgroups: comp.lang.c Subject: Re: Pointer Comparison and Portability Message-ID: <636@sdchema.sdchem.UUCP> Date: Sun, 15-Feb-87 06:27:52 EST Article-I.D.: sdchema.636 Posted: Sun Feb 15 06:27:52 1987 Date-Received: Sun, 15-Feb-87 16:00:33 EST References: <416@vaxine.UUCP> <454@mipos3.UUCP> Sender: news@sdchem.UUCP Reply-To: tps@sdchemf.UUCP (Tom Stockfisch) Organization: UC San Diego Lines: 61 In article <454@mipos3.UUCP> pinkas@mipos3.UUCP (Israel Pinkas) writes: >In article <416@vaxine.UUCP> nw@vaxine.UUCP (Neil Webber) writes: >>Consider the following C function: >> >> same_char (p, q) >> char *p; >> char *q; >> { >> return (p == q); >> } >> >>Does this function only return a non-zero value when p and q point >>to the same physical character? This may seem like a silly question, >>but I haven't found an iron-clad answer in K & R yet. I quote: >... >True. Consider the 80x86 achitecture and many of the existing C ompilers >for it. Many of them try to optimize in memory models with large data >segments by using as small an offset as possible for the first element of >the array. (This is a very small optimization, but it does save some >comparisons if the array is known to be smaller than a certain size.) For >example: > > char p[16], q[16]; >... >Considering that K&R specically said that only pointers to the same array >should be compared, and the fact that many C compilers take K&R to be the >final word on everything, I would say that unless your compiler manual >states otherwise (or it works), you should avoid comparing pointers to >different arrays. >-Israel I would say that a function call which compared its two pointer arguments would *have* to work, no matter how K&R are interpreted on this point, or else strcpy wouldn't work. Proof: Using your declaration as a basis, declare char p[16]; char q[] = "stRing q"; strcpy( p, q ); now (assuming strcpy() is a real function call) look at strcpy(): char * strcpy( s1, s2 ) char *s1, *s2; If (s1 == s2) could be true even if s1 and s2 pointed to different areas, then strcpy would copy s2 on top of itself. That is, even on a segmented architecture, when pointers get passed to a subroutine, they MUST have distinct addresses -- how else can the subroutine know what area of memory to access? "<" and ">" are still another matter. || Tom Stockfisch, UCSD Chemistry tps%chem@sdcsvax.UCSD