strchr with its second argument = 0

T

Thomas Richter

Hi folks,

what is the behavior of strchr if the second argument (the character to
be searched for) is set to zero? Does it find the end of the string, or
does it find nothing and should it return NULL?

Thanks,
Thomas
 
R

Ralf Damaschke

Thomas Richter said:
what is the behavior of strchr if the second argument (the
character to be searched for) is set to zero? Does it find the
end of the string, or does it find nothing and should it return
NULL?

The definition says:

| The strchr function locates the first occurrence of c (converted
| to a char) in the string pointed to by s. The terminating null
| character is considered to be part of the string.

So it should return a pointer to the end of the string.

-- Ralf
 
K

Kaz Kylheku

Hi folks,

what is the behavior of strchr if the second argument (the character to
be searched for) is set to zero? Does it find the end of the string, or
does it find nothing and should it return NULL?

It should find nothing and return null. The null character isn't a character
which is in the string. By definition of a C string, a C string cannot contain
the null character.
 
K

Kaz Kylheku

It should find nothing and return null. The null character isn't a character
which is in the string. By definition of a C string, a C string cannot contain
the null character.

Sorry; that's wrong! I was staring at the definition in C99, and somehow read
that as "the terminating null character is *not* considered part of the
string"; but the word "not" is not there.

(Of course; why would such text even be included other than to give a special
exception to the definition of a string.)

Still, this text actually poor. "Part of the string": does that mean part of
the representation of the string, or part of the string data? Of course a null
is part of every correct C string. But it's not considered part of the abstract
string. The intent is clear though.
 
K

Kaz Kylheku

Such text in the definition of strchr, would be included
to remind you of how a string is actually defined.

ISO/IEC 9899:1999 (E)
7. Library
7.1 Introduction
7.1.1 Definitions of terms
1 A string is a contiguous sequence of characters
terminated by
and including the first null character.

That is true of the representation of a string. To store a four character
string we have to allocate five bytes and copy five bytes, after all. If we do
not reserve that space, or ensure that the null is propagated somehow, then we
do not have a string object.

But, on the other hand, strlen("abc") is 3. Only those characters prior to the
nul are counted, and that quantity is called the length of the string.
Something outside of the measured length of an object is not part of that
object. Or at best it is ambiguous. (For instance, the height of some building,
for some given purpose, may or may not exclude an antenna mast on top.)

If strings had a header indicating their length, followed by the data then
7.1.1 would have define a string as being that header and that data.

It is highly implausible that the text in strchr is given for the purpose of
reminding the reader what a string is. It is to give a requirement of behavior
which only comes into play when the search character is null.

In fact, this kind of text is given in quite a few places. In contexts where it
may not be clear, there is a reminder "including the terminating null
character". Furthermore, in a few places, there is the opposite reminder: "not
including the terminating null character". So, which of those places are
the reminders about how a string is defined, and which are the exceptions?

What these are are reminders of the definition of a string is two-fold and
ambiguous: it is either the abstract sequence of characters, or the storage
object, depending on context.

Consider stpbrk. Its definition has no reminder. What should strpbrk("", "")
return? If 7.1.1 gives the definition of an abstract string, then "" must be
considered to be the string consisting of the null character. The "" passed as
a right argument must denote a set which contains the null character, and since
that character is found in the left argument, a pointer should be returned
to the left argument's null. But, in fact, the return value in this case is
the null pointer. An empty string denotes an empty set, and a string in which
no character can be found.

Likewise, if that text about the null character were not given in the
definition of strchr, the interpretation would have to be that the null
character is never found, and so a null pointer is returned. A null character
normally cannot be found in a string because the search terminates when the
terminator is encountered. Null-finding behavior is an exception.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top