C Library Function - memchr()
C Standard Library - <string.h>
Description
The C library function void *memchr(const void *str, int c, size_t n) searches for the first occurrence of the character c (an unsigned char) in the first n bytes of the string pointed to by the argument str.
Declaration
Here is the declaration for the memchr() function.
void *memchr(const void *str, int c, size_t n)
Parameters
str -- A pointer to the memory block to be searched.
c -- The value to be passed as an int, but the function performs a byte search using the unsigned char conversion of this value.
n -- The number of bytes to be analyzed.
Return Value
The function returns a pointer to the matching byte, or NULL if the character does not appear in the given memory area.
Example
The following example demonstrates the use of the memchr() function.
#include <stdio.h>
#include <string.h>
int main ()
{
const char str[] = "http://www.tutorialpro.org";
const char ch = '.';
char *ret;
ret = (char*)memchr(str, ch, strlen(str));
printf("String after |%c| is - |%s|\n", ch, ret);
return(0);
}
Let's compile and run the above program, which will produce the following result:
String after |.| is - |.tutorialpro.org|