Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #20394 > unrolled thread
| Started by | Thomas Richter <thor@math.tu-berlin.de> |
|---|---|
| First post | 2012-05-04 20:37 +0200 |
| Last post | 2012-05-04 19:39 -0400 |
| Articles | 7 — 4 participants |
Back to article view | Back to comp.lang.c
strchr with its second argument = 0 Thomas Richter <thor@math.tu-berlin.de> - 2012-05-04 20:37 +0200
Re: strchr with its second argument = 0 Ralf Damaschke <rwspam@gmx.de> - 2012-05-04 18:45 +0000
Re: strchr with its second argument = 0 Kaz Kylheku <kaz@kylheku.com> - 2012-05-04 19:49 +0000
Re: strchr with its second argument = 0 Kaz Kylheku <kaz@kylheku.com> - 2012-05-04 19:53 +0000
Re: strchr with its second argument = 0 pete <pfiland@mindspring.com> - 2012-05-04 19:31 -0400
Re: strchr with its second argument = 0 Kaz Kylheku <kaz@kylheku.com> - 2012-05-05 02:22 +0000
Re: strchr with its second argument = 0 pete <pfiland@mindspring.com> - 2012-05-04 19:39 -0400
| From | Thomas Richter <thor@math.tu-berlin.de> |
|---|---|
| Date | 2012-05-04 20:37 +0200 |
| Subject | strchr with its second argument = 0 |
| Message-ID | <jo17lh$g8u$1@news.belwue.de> |
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
[toc] | [next] | [standalone]
| From | Ralf Damaschke <rwspam@gmx.de> |
|---|---|
| Date | 2012-05-04 18:45 +0000 |
| Message-ID | <XnsA049D3336ADC2dammel@y2plugh.fqdn.th-h.de> |
| In reply to | #20394 |
Thomas Richter <thor@math.tu-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
[toc] | [prev] | [next] | [standalone]
| From | Kaz Kylheku <kaz@kylheku.com> |
|---|---|
| Date | 2012-05-04 19:49 +0000 |
| Message-ID | <20120504123326.903@kylheku.com> |
| In reply to | #20394 |
On 2012-05-04, Thomas Richter <thor@math.tu-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!
[toc] | [prev] | [next] | [standalone]
| From | Kaz Kylheku <kaz@kylheku.com> |
|---|---|
| Date | 2012-05-04 19:53 +0000 |
| Message-ID | <20120504123921.569@kylheku.com> |
| In reply to | #20397 |
On 2012-05-04, Kaz Kylheku <kaz@kylheku.com> wrote: > On 2012-05-04, Thomas Richter <thor@math.tu-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!
[toc] | [prev] | [next] | [standalone]
| From | pete <pfiland@mindspring.com> |
|---|---|
| Date | 2012-05-04 19:31 -0400 |
| Message-ID | <4FA466D2.59AD@mindspring.com> |
| In reply to | #20398 |
Kaz Kylheku wrote:
>
> On 2012-05-04, Kaz Kylheku <kaz@kylheku.com> 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.
--
pete
[toc] | [prev] | [next] | [standalone]
| From | Kaz Kylheku <kaz@kylheku.com> |
|---|---|
| Date | 2012-05-05 02:22 +0000 |
| Message-ID | <20120504183934.117@kylheku.com> |
| In reply to | #20401 |
On 2012-05-04, pete <pfiland@mindspring.com> wrote:
> Kaz Kylheku wrote:
>>
>> On 2012-05-04, Kaz Kylheku <kaz@kylheku.com> 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.
[toc] | [prev] | [next] | [standalone]
| From | pete <pfiland@mindspring.com> |
|---|---|
| Date | 2012-05-04 19:39 -0400 |
| Message-ID | <4FA468BF.3F15@mindspring.com> |
| In reply to | #20394 |
Thomas Richter 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?
str_chr returns the same values that strchr returns.
char *
str_chr(const char *s, int c)
{
while (*s != (char)c) {
if (*s == '\0') {
s = NULL;
break;
}
++s;
}
return (char *)s;
}
--
pete
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c
csiph-web