memccpy ( )【C语言库函数源代码】

C语言库函数源代码】

【本程序在Dev C++ 4.9.9.2 下编译通过】

#include <stdlib.h>

/*

   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;

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值