【C語言庫函數原始碼】
【本程式在Dev C++ 4.9.9.2 下編譯通過】
#include <stdlib.h>
/*
Searches at bufferfor the given character, stopping when characteris first found or cnt bytes have been searched through.
從buffer所指記憶體地區的前count個位元組尋找字元ch,當第一次遇到字元ch時停止尋找。
如果成功,返回指向字元ch的指標;否則返回NULL。
*/
void * my_memchr(const void * buffer,int ch,int count)
{
while ( count && (*(unsigned char *)buffer != (unsigned char)ch) )
{
buffer = (unsigned char *)buffer + 1;
count--;
}
return(count ? (void *)buffer : NULL);
}
int main()
{
char *str = "ammana_babi";
char * p;
char ch;
ch = '9';
p = (char *)my_memchr(str,ch,strlen(str)+1);
if(p == NULL)
printf("Can't find the character %c !/n",ch);
else
printf("Find the character %c !/n",*p);
ch = 'b';
p = (char *)my_memchr(str,ch,strlen(str)+1);
if(p == NULL)
printf("Can't find the character %c !/n",ch);
else
printf("Find the character %c !/n",*p);
system("pause");
return 0;
}