Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > possible undefined use of strchr

Reply
Thread Tools

possible undefined use of strchr

 
 
Andrey Vul
Guest
Posts: n/a
 
      10-27-2009
Is use of strchr(const char *s, '\0') undefined as as a short form of
(s + strlen(s)) ?
The problem is that memchr requires strlen, so strchr is the only way
to do uncounted char find. Is there a way to use it so that it returns
the address of the '\0' in s?
 
Reply With Quote
 
 
 
 
Andrey Vul
Guest
Posts: n/a
 
      10-27-2009
On Oct 27, 3:39*pm, Andrey Vul <(E-Mail Removed)> wrote:
> Is use of strchr(const char *s, '\0') undefined as as a short form of
> (s + strlen(s)) ?
> The problem is that memchr requires strlen, so strchr is the only way
> to do uncounted char find. Is there a way to use it so that it returns
> the address of the '\0' in s?


The point of the question is for an implementation of strpcat()
(similar to mempcpy() ), i.e. to allow strcat() chaining (strcat(strcat
(...),...).
 
Reply With Quote
 
 
 
 
Andrey Vul
Guest
Posts: n/a
 
      10-27-2009
On Oct 27, 3:50*pm, Richard Heathfield <(E-Mail Removed)> wrote:
> In <(E-Mail Removed)>,
>
> Andrey Vul wrote:
> > Is use of strchr(const char *s, '\0') undefined as as a short form
> > of (s + strlen(s)) ?

>
> No, that's fine. The Standard specifically provides for this
> possibility:
>
> "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."
>


Oh, strchr returns NULL when the sentinel zero of the string has been
reached _and_ second argument is not zero?
 
Reply With Quote
 
Andrey Vul
Guest
Posts: n/a
 
      10-28-2009
On Oct 28, 9:56*am, Mr John FO Evans <(E-Mail Removed)> wrote:
> In article
> <(E-Mail Removed)>, AndreyVul <(E-Mail Removed)> wrote:
>
> om>> The problem is that memchr requires strlen, so strchr is the only way
> om>> to do uncounted char find. Is there a way to use it so that it returns
> om>> the address of the '\0' in s?
>
> Surely strlen(s)+1 will find the \0


That returns the index, not the pointer.
strchr(s, '\0') returns the pointer
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      10-28-2009
Andrey Vul <(E-Mail Removed)> writes:
> On Oct 28, 9:56Â*am, Mr John FO Evans <(E-Mail Removed)> wrote:
>> In article
>> <(E-Mail Removed)>, AndreyVul <(E-Mail Removed)> wrote:
>>
>> om>> The problem is that memchr requires strlen, so strchr is the only way
>> om>> to do uncounted char find. Is there a way to use it so that it returns
>> om>> the address of the '\0' in s?
>>
>> Surely strlen(s)+1 will find the \0

>
> That returns the index, not the pointer.
> strchr(s, '\0') returns the pointer


So does s + strlen(s).

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
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