P
pete
pete said:Peter said:it does not make comparisons based on unsigned char values.
[A requirement for the real strcmp().]
I don't see that in the standard.
OK, now I do.
pete said:Peter said:it does not make comparisons based on unsigned char values.
[A requirement for the real strcmp().]
I don't see that in the standard.
Peter said:nrk said:Ok, here's my take on this:
a) sizeof(int) > 1 for hosted implementations.
http://www.google.com/[email protected]
There mere fact that Dan Pop says so does not make it so!
There are actual members of the C Committee who disagree on this.
So, integer overflow not an issue, yes?
No. Even if sizeof(int) == 2, you can still have INT_MAX < UCHAR_MAX.
[It's the limits which are important, not the byte size.]
No. Reading chars via an unsigned char lvalue can produce a different
value to the original.
Peter said:No. Even if sizeof(int) == 2, you can still have INT_MAX < UCHAR_MAX. [It's
the limits which are important, not the byte size.]
b) Peter's concern still remains. So, does changing the last line to:
return *(unsigned char *)s1 - *(unsigned char *)s2;
make it alright?
No. Reading chars via an unsigned char lvalue
can produce a different value to the original.
Since character constants and I/O are based on
unsigned char -> int -> char
_conversions_ when storing plain char strings,
the correct answer (assuming no integer overflow)
is to use a _conversion_ of the plain char value to
unsigned char...
return (unsigned char) *s1 - (unsigned char) *s2;
Note that on most implementations
(8-bit, 2c, no padding) there is no need
to go to this extreme, although the result is the same.
The most robust answer would seem to be...
return (unsigned char) *s1 > (unsigned char) *s2
- (unsigned char) *s1 < (unsigned char) *s2;
or...
return (unsigned char) *s1 < (unsigned char) *s2 ? -1
: (unsigned char) *s1 > (unsigned char) *s2;
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.