possible undefined use of strchr

A

Andrey Vul

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?
 
A

Andrey Vul

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
(...),...).
 
A

Andrey Vul

In <[email protected]>,



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?
 
A

Andrey Vul

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
 

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,766
Messages
2,569,569
Members
45,044
Latest member
RonaldNen

Latest Threads

Top