|
//:Vc++6.0 String memchr函數(shù)
//功能:查找里面第一個(gè)和目標(biāo)字符匹配的字符
//參數(shù):ptr 首地址 value 想要查找的字符值 num 查到那個(gè)地址
//返回值:如果成功,返回指向字符的指針;否則返回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中的運(yùn)行結(jié)果為:'e' found at position 2.
//注:在VC++6.0中函數(shù)只能比較字符串//:~
|
|