函数原型如下:
[Plain Text] 纯文本查看 复制代码 void *memchr(const void *s, int c, size_t n);
第 1 个参数指向要字符串所在的缓冲区,第 2 个参数是要查找的字符串,第 3 个参数说明要在前 n 个字符串里查找。函数执行成功返回查找大的第 1 个匹配字符的位置,否则返回 NULL 。
示例:
[C++] 纯文本查看 复制代码
#include <stdio.h>
#include <string.h>
int ch = 'w';
char string[] = "hello world";
int main()
{
char *pdest;
int result;
printf ("string to be searched:\n\t\t%s\n", string);
pdest = memchr (string, ch, sizeof(string));
if (pdest != NULL) {
result = pdest - string + 1;
printf ("Result:\t\t%c found at position %d\n", ch, result);
}else
printf ("Result\t\t%c not found\n");
return 0;
}
运行输出:
$ ./memchar
string to be searched:
hello world
Result: w found at position 7 |