Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why there is no strnchr function?

Tags:

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?

like image 851
Thomson Avatar asked Jun 28 '14 12:06

Thomson


People also ask

What does Strchr return if not found?

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 care Star Strchr?

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.

Why does Strchr take an int?

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 .

What is the difference between Strchr and Strrchr?

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.


2 Answers

It does exist -- it is called memchr:

http://en.wikipedia.org/wiki/C_string_handling

like image 180
Soren Avatar answered Sep 27 '22 20:09

Soren


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.

like image 26
Kerrek SB Avatar answered Sep 27 '22 21:09

Kerrek SB