//:Vc++6.0 String memchr函數
//功能:查找里面第一個和目標字符匹配的字符
//參數:ptr 首地址 value 想要查找的字符值 num 查到那個地址
//返回值:如果成功,返回指向字符的指針;否則返回NULL
#include<stdio.h>
const void *memchr (const void *ptr, int value, int num);
int main(void)
{
char * pch;
char str[] = "hello world";
pch = (char*) memchr (str, 'e', 12);
if (pch!=NULL)
printf ("'e' found at position %d.\n", pch - str + 1);
else
printf ("'e' not found.\n");
return 0;
}
const void *memchr (const void *ptr, int value, int num)
{
if (ptr == NULL)
{
perror("ptr");
return NULL;
}
char * p = (char *)ptr;
while (num--)
{
if (*p != (char)value)
p++;
else
return p;
}
return NULL;
}
//在vc++6.0中的運行結果為:'e' found at position 2.
//注:在VC++6.0中函數只能比較字符串//:~
|