Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1396155
  • 博文数量: 143
  • 博客积分: 10005
  • 博客等级: 上将
  • 技术积分: 1535
  • 用 户 组: 普通用户
  • 注册时间: 2006-10-23 17:25
个人简介

淡泊明志 宁静致远

文章分类

全部博文(143)

文章存档

2011年(2)

2009年(1)

2007年(22)

2006年(118)

我的朋友

分类: C/C++

2006-11-25 12:36:45

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;

}

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