分类: C/C++
2008-05-31 15:39:18
【C语言库函数源代码】
【本程序在Dev C++ 4.9.9.2 下编译通过】
#include
/*
Copies bytes from src to dest until count bytes have been copied,or up to and including the character c, whichever comes first.
如果src前n个字节中存在’c’,返回指向字符’c’后的第一个字符的指针;
*/
void * my_memccpy(void *dest,const void *src,int c,int count)
{
while ( count && (*((char *)(dest = (char *)dest + 1) - 1) =
*((char *)(src = (char *)src + 1) - 1)) != (char)c )
count--;
return(count ? dest : NULL);
}
/*这个函数的while条件判断写的比较长,看的眼疼,等价与以下写法:*/
void * my_memccpy01(void *dst,const void *src,int c,int count)
{
while (count)
{
*(char *)dst = *(char *)src;
dst = (char *)dst + 1;
if(*(char *)src == (char) c)
break;
src = (char *)src + 1;
count--;
}
return(count ? dst : NULL);
}
int main()
{
char a[12];
char * p;
char * str ="ammana_babi";
char ch;
ch = '9';
p = (char *)my_memccpy01(a,str,ch,strlen(str)+1);
if(p == NULL)
printf("\nCan't not find character. \n");
else
{
printf("\nFind the character! \n");
*p= '\0';
}
printf("\nThe String which has been copied is:\t");
puts(a);
printf("************************************");
ch = 'b';
p = (char *)my_memccpy01(a,str,ch,strlen(str)+1);
if(p == NULL)
printf("\nCan't not find character. \n");
else
{
printf("\nFind the character! \n");
*p = '\0';
}
printf("\nThe String which has been copied is:\t");
puts(a);
system("pause");
return 0;