Locates a character in a buffer.
Include
<string.h>
Prototype
void *memchr(const void *s, int c, size_t
n);
Arguments
s
c
n
Return Value
Returns a pointer to the location of the match if successful; otherwise,
returns NULL
.
Remarks
memchr
stops when it finds the first occurrence of
c
, or after searching n number of characters.
Example
#include <string.h>
#include <stdio.h>
int main(void)
{
char buf1[50] = "What time is it?";
char ch1 = 'i', ch2 = 'y';
char *ptr;
int res;
printf("buf1 : %s\n\n", buf1);
ptr = memchr(buf1, ch1, 50);
if (ptr != NULL)
{
res = ptr - buf1 + 1;
printf("%c found at position %d\n", ch1, res);
}
else
printf("%c not found\n", ch1);
printf("\n");
ptr = memchr(buf1, ch2, 50);
if (ptr != NULL)
{
res = ptr - buf1 + 1;
printf("%c found at position %d\n", ch2, res);
}
else
printf("%c not found\n", ch2);
}
Example Output
buf1 : What time is it?
i found at position 7
y not found