Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > strchr with its second argument = 0

Reply
Thread Tools

strchr with its second argument = 0

 
 
Thomas Richter
Guest
Posts: n/a
 
      05-04-2012
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
 
Reply With Quote
 
 
 
 
Ralf Damaschke
Guest
Posts: n/a
 
      05-04-2012
Thomas Richter <(E-Mail Removed)-berlin.de> wrote:

> 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
 
Reply With Quote
 
 
 
 
Kaz Kylheku
Guest
Posts: n/a
 
      05-04-2012
On 2012-05-04, Thomas Richter <(E-Mail Removed)-berlin.de> wrote:
> 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.

--
If you ever need any coding done, I'm your goto man!
 
Reply With Quote
 
Kaz Kylheku
Guest
Posts: n/a
 
      05-04-2012
On 2012-05-04, Kaz Kylheku <(E-Mail Removed)> wrote:
> On 2012-05-04, Thomas Richter <(E-Mail Removed)-berlin.de> wrote:
>> 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.


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.

--
If you ever need any coding done, I'm your goto man!
 
Reply With Quote
 
Kaz Kylheku
Guest
Posts: n/a
 
      05-05-2012
On 2012-05-04, pete <(E-Mail Removed)> wrote:
> Kaz Kylheku wrote:
>>
>> On 2012-05-04, Kaz Kylheku <(E-Mail Removed)> wrote:

>
>> > 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.)

>
> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
strchr declaration ts C Programming 2 03-14-2007 09:34 AM
strchr & finding multiple occurances of a char David Warner C Programming 4 04-26-2005 03:45 PM
a shorter strchr Servé Lau C Programming 9 02-19-2004 02:56 AM
C++ strings and strchr() ataru@nospam.cyberspace.org C++ 32 08-22-2003 05:33 PM
Re: Question about strchr() / strrchr() Martin Dickopp C Programming 4 08-16-2003 07:42 PM



Advertisments