Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2833317
  • 博文数量: 599
  • 博客积分: 16398
  • 博客等级: 上将
  • 技术积分: 6875
  • 用 户 组: 普通用户
  • 注册时间: 2009-11-30 12:04
个人简介

WINDOWS下的程序员出身,偶尔也写一些linux平台下小程序, 后转行数据库行业,专注于ORACLE和DB2的运维和优化。 同时也是ios移动开发者。欢迎志同道合的朋友一起研究技术。 数据库技术交流群:58308065,23618606

文章分类

全部博文(599)

文章存档

2014年(12)

2013年(56)

2012年(199)

2011年(105)

2010年(128)

2009年(99)

分类: C/C++

2012-01-05 12:35:27

bcmp

 

原型:extern int bcmp(const void *s1, const void *s2, int n);

 

用法:#include

 

功能:比较字符串s1s2的前n个字节是否相等

 

说明:如果s1=s2n=0则返回零,否则返回非零值。bcmp不检查NULL

 

举例:

 

/home/cpicsrv/yansp/cstring > cat bcmp.c

//bcmp.c

#include

#include

int main(void)

{

  char *s1="Hello Huateng!";

  char *s2="Hello huateng!";

  int  result;

 

  result=bcmp(s1,s2,6);

  printf("Result=[%d]\n",result);

  if (result==0)

   printf("String \"%s\" and  \"%s\" is  equal in first 6 bytes! \n",s1,s2);

  else

  printf("String \"%s\" and  \"%s\" is  not equal in first 6 bytes! \n",s1,s2);

 

  result=bcmp(s1,s2,13);

  printf("Result=[%d]\n",result);

  if (result==-1)

   printf("String \"%s\"  is less than \"%s\"  in first 13 bytes! \n",s1,s2);

  else

   printf("String \"%s\"  is greater than  \"%s\"  in first 13 bytes! \n",s1,s2);

  

  result=bcmp(s2,s1,13);

  printf("Result=[%d]\n",result);

  if (result==1)

      printf("String \"%s\"  is greater than  \"%s\"  in first 13 bytes! \n",s1,s2);

  else

      printf("String \"%s\"  is less than \"%s\"  in first 13 bytes! \n",s1,s2);

  

  return 0;

}

/home/cpicsrv/yansp/cstring > gcc -Wall bcmp.c -o bcmp

/home/cpicsrv/yansp/cstring > ./bcmp

Result=[0]

String "Hello Huateng!" and  "Hello huateng!" is  equal in first 6 bytes!

Result=[-1]

String "Hello Huateng!"  is less than "Hello huateng!"  in first 13 bytes!

Result=[1]

String "Hello Huateng!"  is greater than  "Hello huateng!"  in first 13 bytes!

/home/cpicsrv/yansp/cstring >

 

bcopy

 

  原型:extern void bcopy(const void *src, void *dest, int n);

 

  用法:#include

 

  功能:将字符串src的前n个字节复制到dest

 

  说明:bcopy不检查字符串中的空字节NULL,函数没有返回值。

 

  举例:

 

 

/home/cpicsrv/yansp/cstring > cat bcopy.c

//bcopy.c

#include

#include

int main()

{

  char *s1="Hello huateng!";

  char s2[50];

 

  bcopy(s1,s2,5);

  printf("s1=[%s]\n",s1);

  printf("s2=[%s]\n",s2);

 

  return 0;

}

/home/cpicsrv/yansp/cstring > gcc -Wall bcopy.c -o bcopy

/home/cpicsrv/yansp/cstring > ./bcopy

s1=[Hello huateng!]

s2=[Hello]

/home/cpicsrv/yansp/cstring >

 

bzero

 

  原型:extern void bzero(void *s, int n);

 

  用法:#include

 

  功能:置字节字符串s的前n个字节为零。

 

  说明:bzero无返回值。

 

  举例:

 

/home/cpicsrv/yansp/cstring > cat bzero.c

//bzero.c

#include

#include

int main()

{

  struct

    {

      int a;

      float b;

      char  c[20];

     } huateng;

   huateng.a=10;

   huateng.b=20.322;

   strcpy(huateng.c,"HUATENG");

   printf("a=[%d]\n",huateng.a);

   printf("b=[%f]\n",huateng.b);

   printf("c=[%s]\n",huateng.c);

  

   bzero(&huateng,sizeof(huateng));

  

   printf("a=[%d]\n",huateng.a);

   printf("b=[%f]\n",huateng.b);

   printf("c=[%s]\n",huateng.c);

  

   return 0;

}

/home/cpicsrv/yansp/cstring > gcc -Wall bzero.c -o bzero

/home/cpicsrv/yansp/cstring > ./bzero

a=[10]

b=[20.322001]

c=[HUATENG]

a=[0]

b=[0.000000]

c=[]

/home/cpicsrv/yansp/cstring >

 

memccpy

1.4.  memccpy

 

  原型:extern void *memccpy(void *dest, void *src, unsigned char ch, unsigned int count);

 

  用法:#include

 

  功能:由src所指内存区域复制不多于count个字节到dest所指内存区域,如果遇到字符ch则停止复制。

 

  说明:返回指向字符ch后的第一个字符的指针,如果srcn个字节中不存在ch则返回NULL,并且源字符串会被整个复制到目标字符串中。 

 

  举例:

 

/home/cpicsrv/yansp/cstring > cat memccpy.c

//memccpy

#include

#include

int main()

{

  char *s="Hello -Huateng!";

  char d[20],*p;

 

  p=memccpy(d,s,'-',strlen(s));

  printf("Address of p is :[%x]\n",p);

  printf("Address of p is :[%x]\n",d);

  if(p)

  {

    *p='\0';

    printf("string d is :[%s]\n",d);

  }

  else

   printf("Char not found!");

  

 p=memccpy(d,s,'$',strlen(s));

 if (!p)

 printf("Char not found!  string d is :[%s]\n",d);

 

  return 0;

}

/home/cpicsrv/yansp/cstring > gcc -Wall memccpy.c -o  memccpy

memccpy.c: In function `main':

memccpy.c:10: warning: unsigned int format, pointer arg (arg 2)

memccpy.c:11: warning: unsigned int format, pointer arg (arg 2)

/home/cpicsrv/yansp/cstring > ./memccpy

Address of p is :[bff7fbd7]

Address of p is :[bff7fbd0]

string d is :[Hello -]

Char not found!  string d is :[Hello -Huateng]

/home/cpicsrv/yansp/cstring >

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