Is this a correct implementation of strstr ?

P

Phil Carmody

Keith Thompson said:
You're probably right as far as the intent is concerned, but I think
the wording of the standard is internally inconsistent.

That's one way of putting it. I'd almost go as far as to say it's
broken, as it requires something to have the same value as something
that doesn't exist in this case.

Phil
 
I

Ian Collins

Ian Collins a écrit :


I don't work out how you make compatible things both "superfluous" and
"wrong" : usually, superfluous suppose things don't hurt, this is not
usually the case for something _wrong_.

Do you mean the casts are always superfluous and sometimes wrong ?

What I mean is given the requirements for strchr,

while ((unsigned char)*s != (unsigned char)c)

casts to the wrong type. Using the cast to the required type, the cast
of s is not required.

while (*s != (char)c)
 
K

Keith Thompson

Phil Carmody said:
The standard doesn't put a condition on the signedness of chars
when it specifies what the value should be, so presumably yes.

It doesn't do so explicitly, but it does define the value of a
character constant in terms of the value of an object of type char.
C99 6.4.4.4p10:

If an integer character constant contains a single character
or escape sequence, its value is the one that results when
an object with type char whose value is that of the single
character or escape sequence is converted to type int.

The value of an object of type char must be in the range
CHAR_MIN..CHAR_MAX (which, if plain char is signed, is the same as
the range SCHAR_MIN..SCHAR_MAX, typically -128..+127).
Mine too. I guess -96 is the value of the a char with value 160 in
this instance.

Yeah, that must be it. :cool:}
 

Ask a Question

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.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,269
Latest member
vinaykumar_nevatia23

Latest Threads

Top