Pointer Comparison and Portability
tps at sdchem.UUCP
tps at sdchem.UUCP
Sun Feb 15 21:27:52 AEST 1987
In article <454 at mipos3.UUCP> pinkas at mipos3.UUCP (Israel Pinkas) writes:
>In article <416 at vaxine.UUCP> nw at 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 at sdcsvax.UCSD
More information about the Comp.lang.c
mailing list