As the other C library function, like strcpy
, strcat
, there is a version which limits the size of string (strncpy
, etc.), I am wondering why there is no such variant for strchr
?
The strchr() function returns a pointer to the first occurrence of c that is converted to a character in string. The function returns NULL if the specified character is not found.
What is the use of function char *strchr(ch, c)? Explanation: The given code char *strchr(ch, c) return pointer to first occurrence of c in ch or NULL if not present.
char *strchr(const char *s, int c); because int is what the above legacy implementation expects to physically receive as c . Declaring it with a char parameter would be incorrect. For this reason, you will never see "traditional" standard library functions expecting parameters of type char , short or float .
The strchr() function returns a pointer to the first occurrence of the character c in the string s. The strrchr() function returns a pointer to the last occurrence of the character c in the string s.
It does exist -- it is called memchr
:
http://en.wikipedia.org/wiki/C_string_handling
In C, the term "string" usually means "null terminated array of characters", and the str*
functions operate on those kinds of strings. The n
in the functions you mention is mostly for the sake of controlling the output.
If you want to operate on an arbitary byte sequence without any implied termination semantics, use the mem*
family of functions; in your case memchr
should serve your needs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With