Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4463545
  • 博文数量: 356
  • 博客积分: 10458
  • 博客等级: 上将
  • 技术积分: 4734
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-24 14:59
文章分类

全部博文(356)

文章存档

2020年(17)

2019年(9)

2018年(26)

2017年(5)

2016年(11)

2015年(20)

2014年(2)

2013年(17)

2012年(15)

2011年(4)

2010年(7)

2009年(14)

2008年(209)

分类: 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’后的第一个字符的指针;否则返回NULL,src被复制。

*/

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;

}
阅读(3199) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~