C语言学习之字符串、字符和字节第三讲

1.错误信息

        1.1函数原型:char *strerror( int  error_number );

            函数功能:当你调用一些函数时,请求操作系统执行一些功能如打开文件时,如果出现错误,操作系统是通过设置一个外部整型变量errno进行错误代码报告的。strerror函数是吧其中的一个错误代码作为参数并返回一个指向用于描述错误的字符串的指针。事实上返回值应该被声明为const,因为你不应该修改它。

#include <stdio.h> // void perror(const char *msg);

#include <string.h> // char *strerror(int errnum);
#include <errno.h> //errno
perror是错误输出函数,输出格式为:msg:errno对应的错误信息(加上一个换行符);
strerror 是通过参数 errnum (就是errno),返回对应的错误信息。

errno 是错误代码,在 errno.h头文件中;

以下是测试程序:

--------------------------------------------------------------------

//  p_str_error.c
//  perror , strerror 函数 , errno 测试

#include <stdio.h>
#include <stdlib.h>
#include < string.h>
#include <errno.h>

int main( int argc,  char *argv[])
{
 FILE *fp;
 char *buf;

 if( (fp = fopen(argv[1], "r")) == NULL)
 {
  perror("perror"); // 好方便
  errno = 12;
  printf("strerror: %s\n", strerror(errno)); //转换错误码为对应的错误信息
  exit(1);
 }

 perror("perror");
 errno = 13;
 printf("strerror: %s\n", strerror(errno));
 
 fclose(fp); 
 return 0;
}



--------------------------------------------------------------------

输入一个存在的文件名,如:./a.out 111
perror: No such file or directory
strerror: Cannot allocate memory
perror: Success
strerror: Permission denied

open失败则会输出:

open成功则会输出:

很明显,perror信息是由 perror函数输出的了,第二行是 strerror通过将 errno 轮换成对应的错误信息打印出来。

参考链接Standard C 语言标准函数库速查

               用户组函数

2.字符操作

      2.1字符分类

C语言中<ctype.h> ,在C++中为<cctype>
本文下面列出这个头文件的常用函数。目的是了解字符判断函数和学习简洁代码编写。
注意:C++中的<cctype>和<cwtype>头文件继承于C。在许多实现方式中,函数在s记命名空间内部和外部都定义了,
以允许旧式C程序编译和链接。此时,函数名无论是否带std限定符都能工作,但因为我们编写的是C++程序,所以应
限定名称。
isalnum() 如果参数为字母数字,则返回ture
isalpha() 如果参数为字母,则返回ture
iscntrl() 如果参数为控制字符,则返回ture
isdigit() 如果参数为数字(0-9),则返回ture
isgraph() 如果参数为空格之外的打印字符,则返回ture
islower() 如果参数是小写字母,则返回ture
isprint() 如果参数为打印字符,包括空格,则返回ture
ispunct() 如果参数为标点符号,则返回ture
isspace() 如果参数为标准空白字符(空格,进纸,换行符,回车,水平制表符,垂直制表符),则返回ture
siupper() 如果参数为大写字母,则返回ture
isxdigit() 如果参数为十六进制数,则返回ture
tolower() 如果参数为大写字母,即转化为小写字母,否则返回原值
toupper() 如果参数为小写字母,即转化为大写字母,否则返回原值


ctype.h的字符判断函数 //   *******************************************************
ctype.h的字符判断函数
//   判断字符c是否为英文字母:::0x41 0x61,如果是大写字母,
ctype.h的字符判断函数
//   将通过(ch | 0x20)转为小写字母值来判断
ctype.h的字符判断函数
int   isalpha( int   ch )
ctype.h的字符判断函数
{
ctype.h的字符判断函数
 return (unsigned int)((ch | 0x20) - 'a') < 26u;
ctype.h的字符判断函数 }

ctype.h的字符判断函数
ctype.h的字符判断函数
//   *******************************************************
ctype.h的字符判断函数
//   判断字符c是否为大写英文字母
ctype.h的字符判断函数
int   isupper(   int   ch )
ctype.h的字符判断函数
{
ctype.h的字符判断函数
 return (unsigned int)(ch - 'A') < 26u;
ctype.h的字符判断函数 }

ctype.h的字符判断函数
ctype.h的字符判断函数
//   *******************************************************
ctype.h的字符判断函数
//   判断字符c是否为小写英文字母
ctype.h的字符判断函数
int   islower (   int   ch )
ctype.h的字符判断函数
{
ctype.h的字符判断函数
 return (unsigned int) (ch - 'a') < 26u;
ctype.h的字符判断函数 }

ctype.h的字符判断函数
ctype.h的字符判断函数
//   *******************************************************
ctype.h的字符判断函数
//   判断字符c是否为数字
ctype.h的字符判断函数
int   isdigit(   int   ch )
ctype.h的字符判断函数
{
ctype.h的字符判断函数
 return (unsigned int)(ch - '0') < 10u;
ctype.h的字符判断函数 }

ctype.h的字符判断函数
ctype.h的字符判断函数
//   *******************************************************
ctype.h的字符判断函数
//   判断字符c是否为十六进制数字。
ctype.h的字符判断函数
//   当c为A-F,a-f或0-9之间的十六进制数字时,返回非零值。
ctype.h的字符判断函数
int   isxdigit(   int   ch )
ctype.h的字符判断函数
{
ctype.h的字符判断函数
 return (unsigned int)( ch -'0') < 10u || 
ctype.h的字符判断函数 (unsigned
 int)((ch | 0x20) - 'a') < 6u;
ctype.h的字符判断函数 }

ctype.h的字符判断函数
ctype.h的字符判断函数
//   *******************************************************
ctype.h的字符判断函数
//   判断字符c是否为空白符。空白符指空格、水平制表、垂直制表、换页、回车和换行符。
ctype.h的字符判断函数
int   isspace(   int   ch )
ctype.h的字符判断函数
{
ctype.h的字符判断函数
 return (unsigned int)(ch - 9) < 5u || ch == ' ';
ctype.h的字符判断函数 }

ctype.h的字符判断函数
ctype.h的字符判断函数
//   *******************************************************
ctype.h的字符判断函数
//   判断字符c是否为标点符号。标点符号指那些既不是字母数字,也不是空格的可打印字符。
ctype.h的字符判断函数
int   ispunct(   int   ch )
ctype.h的字符判断函数
{
ctype.h的字符判断函数
 return isprint(ch) && !isalnum (ch) && !isspace (ch);
ctype.h的字符判断函数 }

ctype.h的字符判断函数
ctype.h的字符判断函数
//   *******************************************************
ctype.h的字符判断函数
//   测试参数ch是否是字母(A-Z,大小写均可)或数字(0-9)
ctype.h的字符判断函数
int   isalnum (   int   ch )
ctype.h的字符判断函数
{
ctype.h的字符判断函数
 return (unsigned int)((ch | 0x20) - 'a') < 26u ||
ctype.h的字符判断函数 (unsigned
 int)( ch - '0') < 10u;
ctype.h的字符判断函数 }

ctype.h的字符判断函数
ctype.h的字符判断函数
//   *******************************************************
ctype.h的字符判断函数
//   判断字符c是否为可打印字符(含空格)。当c为可打印字符(0x20-0x7e)时,
ctype.h的字符判断函数
//   返回非零值,否则返回零。
ctype.h的字符判断函数
int   isprint(   int   ch )
ctype.h的字符判断函数
{
ctype.h的字符判断函数
 return (unsigned int)(ch - ' ') < 127u - ' ';
ctype.h的字符判断函数 }

ctype.h的字符判断函数
ctype.h的字符判断函数
//   *******************************************************
ctype.h的字符判断函数
//   判断字符c是否为除空格外的可打印字符。可打印字符(0x21-0x7e)
ctype.h的字符判断函数
int   isgraph(   int   ch )
ctype.h的字符判断函数
{
ctype.h的字符判断函数
 return (unsigned int)(ch - '!') < 127u - '!';
ctype.h的字符判断函数 }

ctype.h的字符判断函数
ctype.h的字符判断函数
//   *******************************************************
ctype.h的字符判断函数
//   判断字符c是否为控制字符。当c在0x00-0x1F之间或等于0x7F(DEL)时,
ctype.h的字符判断函数
//   返回非零值,否则返回零。
ctype.h的字符判断函数
int   iscntrl(   int   ch )
ctype.h的字符判断函数
{
ctype.h的字符判断函数
 return (unsigned int)ch < 32u || ch == 127;
ctype.h的字符判断函数 }

ctype.h的字符判断函数
ctype.h的字符判断函数
//   *******************************************************
ctype.h的字符判断函数
//   小写字母转换为大写字母。
ctype.h的字符判断函数
int   toupper(   int   ch)
ctype.h的字符判断函数
{
ctype.h的字符判断函数
 if ( (unsigned int)(ch - 'a') < 26u )
ctype.h的字符判断函数 ch
 += 'A' - 'a';
ctype.h的字符判断函数
 return ch;
ctype.h的字符判断函数 }

ctype.h的字符判断函数
//   返回一个按位与不就行了吗??简练为好,不做检测
ctype.h的字符判断函数
//   return (unsigned int)(ch | 0x20);
ctype.h的字符判断函数
//   tolower 类似表示为 return (unsigned int)(ch & 0x5F);
ctype.h的字符判断函数
ctype.h的字符判断函数
//   *******************************************************
ctype.h的字符判断函数
//   大写字母转换为小写字母。
ctype.h的字符判断函数
int   tolower(   int   ch)
ctype.h的字符判断函数
{
ctype.h的字符判断函数
 if ( (unsigned int)(ch - 'A') < 26u )
ctype.h的字符判断函数 ch
 += 'a' - 'A';
ctype.h的字符判断函数
 return ch;
ctype.h的字符判断函数 }

ctype.h的字符判断函数
ctype.h的字符判断函数
//   *******************************************************
ctype.h的字符判断函数
//   判断字符c是否为ascii码。ascii码指0x00-0x7F之间的字符。
ctype.h的字符判断函数
int   isascii(   int   ch )
ctype.h的字符判断函数
{
ctype.h的字符判断函数
 return (unsigned int)ch < 128u;
ctype.h的字符判断函数 }

ctype.h的字符判断函数
ctype.h的字符判断函数
// *******************************************************
ctype.h的字符判断函数
// 将字符c转换为ascii码。toascii函数将字符c的高位清零,仅保留低七位。返回转换后的数值。
ctype.h的字符判断函数
int   toascii(   int   c)
ctype.h的字符判断函数
{
ctype.h的字符判断函数
 return c & 0x7f;
ctype.h的字符判断函数 }

ctype.h的字符判断函数
ctype.h的字符判断函数
// *******************************************************
ctype.h的字符判断函数
// 判断字符c是否为英文字母和下划线
ctype.h的字符判断函数
int   iscsymf( int   c)
ctype.h的字符判断函数
{
ctype.h的字符判断函数
 return (isalpha(c) || ( c == '_' ));
ctype.h的字符判断函数 }

ctype.h的字符判断函数
ctype.h的字符判断函数
//   *******************************************************
ctype.h的字符判断函数
//   判断字符c是否为英文字母、数字和下划线
ctype.h的字符判断函数
int   iscsym( int   c)
ctype.h的字符判断函数
{
ctype.h的字符判断函数
 return (isalnum(c) || ( c == '_' ));
ctype.h的字符判断函数 }
字符判断函数

      2.2字符转换

           函数原型:int tolower ( int ch);

           函数功能: 函数把大写字母转换为小写字母,tolower函数函数返回其参数的对应小写形式。如果不是处于大写字母,函数不修改直接返回。

           函数原型:int toupper ( int ch);

           函数功能: 函数把小写字母转换为大写字母,tolower函数函数返回其参数的对应大写形式。如果不是处于小写字母,函数不修改直接返回。

范例:将s 字符串内的大写字母转换成小写字母。

   
   
  1. #include <ctype.h>
  2. main(){
  3. char s[] = "aBcDeFgH12345;!#$";
  4. int i;
  5. printf("before tolower() : %s\n", s);
  6. for(i = 0; i < sizeof(s); i++)
  7. s[i] = tolower(s[i]);
  8. printf("after tolower() : %s\n", s);
  9. }


执行结果:
before tolower() : aBcDeFgH12345;!#$
after tolower() : abcdefgh12345;!#$

3.内存操作

    3.1内存复制

      函数原型: void *memcpy( void  *dst , void  const *src  ,  size_t  length);

      函数功能:memcpy从src的起始位置赋值length个字节到dst的内存起始位置。函数返回一个指向dst第一个未知的指针。

  1. /* 
  2. *copyright@nciaebupt 转载请注明出处 
  3. *原型:void *memcpy(void *dest, const void *src, size_t count); 
  4. *用法:#include <cstring> 
  5. *功能:由src所指内存区域复制count个字节到dest所指内存区域。 
  6. *说明:src和dest所指内存区域不能重叠,函数返回指向dest的指针。 
  7. *使用C函数库中的memcpy 
  8. * 
  9. */  
  10. #include <cstdio>  
  11. #include <cstring>  
  12.   
  13. int main(int args,char ** argv)  
  14. {  
  15.     char * src = "Hello world!";  
  16.     char dest[20];  
  17.     /*这里使用strlen(src)+1是因为strlen(src)返回的是src的字符的个数,不包括结束符\0 
  18.     *而我们复制时希望将\0一起复制,故需要将strlen(src)加1 
  19.     */  
  20.     memcpy(dest,src,strlen(src)+1);  
  21.     printf("The dest is : %s\n",dest);  
  22.   
  23.     getchar();  
  24.     return 0;  
  25. }  
  26.   
  27. /* 
  28. *copyright@nciaebupt 转载请注明出处 
  29. *原型:void *memcpy(void *dest, const void *src, size_t count); 
  30. *用法:#include <cstring> 
  31. *功能:由src所指内存区域复制count个字节到dest所指内存区域。 
  32. *说明:src和dest所指内存区域不能重叠,函数返回指向dest的指针。 
  33. *自己实现memcpy 
  34. */  
  35. #include <cstdio>  
  36. #include <cstring>  
  37.   
  38. void * _memcpy(void *dest,const void * src,size_t count)  
  39. {  
  40.     while(count--)  
  41.     {  
  42.         *((char *)dest) = *((char *)src);  
  43.         (char *)(dest = (char *)dest + 1);  
  44.         (char *)(src =(char *)src + 1);  
  45.     }  
  46. }  
  47.   
  48. int main(int args,char ** argv)  
  49. {  
  50.     char * src = "Hello World!";  
  51.     char dest[20];  
  52.     /*这里使用strlen(src)+1是因为strlen(src)返回的是src的字符的个数,不包括结束符\0 
  53.     *而我们复制时希望将\0一起复制,故需要将strlen(src)加1 
  54.     */  
  55.     _memcpy(dest,src,strlen(src) + 1);  
  56.     printf("The length is : %d",strlen(src));  
  57.     printf("The dest is : %s",dest);  
  58.   
  59.     getchar();  
  60.     return 0;  

参考博客C函数库中的memcpy实现

  1. /* 
  2. *copyright@nciaebupt 转载请注明出处 
  3. *c标准库中的函数memccpy 
  4. *原型:extern void *memccpy(void *dest, void *src, unsigned char ch, unsigned int count); 
  5. *用法:#include <string.h> 
  6. *功能:由src所指内存区域复制不多于count个字节到dest所指内存区域,如果遇到字符ch则停止复制。 
  7. *说明:返回指向字符ch后的第一个字符的指针,如果src前n个字节中不存在ch则返回NULL。ch被复制。 
  8. *使用C函数库中的memccpy 
  9. */  
  10.   
  11. #include <cstdio>  
  12. #include <cstring>  
  13.   
  14. int main(int args,char ** argv)  
  15. {  
  16.   
  17.     char *s="Golden Global View";  
  18.     char d[20],*p;  
  19.   
  20.     p = (char *)memccpy(d,s,'e',strlen(s));  
  21.     if(p)  
  22.     {  
  23.         *p='\0';      //给d添加一个结束符\0  
  24.         printf("Char found: %s\n",d);  
  25.     }  
  26.     else  
  27.         printf("Char not found.\n");  
  28.   
  29.   
  30.     getchar();  
  31.     return 0;  
  32. }  
  33.   
  34. /* 
  35. *copyright@nciaebupt 转载请注明出处 
  36. *c标准库中的函数memccpy 
  37. *原型:extern void *memccpy(void * dest,const void * src,int c,unsigned count); 
  38. *用法:#include <string.h> 
  39. *功能:由src所指内存区域复制不多于count个字节到dest所指内存区域,如果遇到字符ch则停止复制。 
  40. *说明:返回指向字符ch后的第一个字符的指针,如果src前n个字节中不存在ch则返回NULL。ch被复制。 
  41. *自己重写memccpy 
  42. */  
  43. #include <cstdio>  
  44. #include <cstring>  
  45.   
  46. void * _memccpy(void * dest, const void * src, int c,unsigned count)  
  47. {  
  48.   
  49.     while(count && (*((char *)(dest = (char *)dest + 1) - 1) =  
  50.                     *((char *)(src = (char *)src + 1) -1)) != (char)c)  
  51.     {  
  52.         count--;  
  53.     }  
  54.   
  55.     return (count ? dest : NULL);  
  56. }  
  57.   
  58. int main(int args,char ** argv)  
  59. {  
  60.     char * src = "Golden Global View";  
  61.     char dest[20],*p;  
  62.     p = (char *)memccpy(dest,src,'d',strlen(src));  
  63.     if(p)  
  64.     {  
  65.         *p = '\0';  
  66.         printf("Char found: %s\n",dest);  
  67.     }  
  68.     else  
  69.     {  
  70.         printf("Char not found.\n");  
  71.     }  
  72.   
  73.     getchar();  
  74.     return 0;  
  75. }  
c库函数memccpy实现

函数返回一个指向

dest

的指针

     3.2内存移动

      函数原型: void *memmove( void  *dst , void  const *src  , size_t  length);

      函数功能:memmove用于从src拷贝count个字符到dst,如果目标区域和源区域有重叠的话,memmove能够保证源串在被覆盖之前将重叠区域的字节拷贝到目标区域中。但复制后src内容会被更改。但是当目标区域与源区域没有重叠则和memcpy函数功能相同。

  1. /* 
  2. *copyright@nciaebupt 转载请注明出处 
  3. *原型:void *memmove(void *dest, const void *src, size_t count); 
  4. *用法:#include <string.h> 
  5. *功能:由src所指内存区域复制count个字节到dest所指内存区域。 
  6. *说明:src和dest所指内存区域可以重叠,但复制后src内容会被更改。函数返回指向dest的指针。 
  7. *使用C函数库中的memmove 
  8. * 
  9. */  
  10. #include <cstdio>  
  11. #include <cstring>  
  12.   
  13. int main(int args,char ** argv)  
  14. {  
  15.     char src[] = "memmove can be very useful......";  
  16.     char dest[40] ={""};  
  17.     memmove(src + 20,src + 15,11);  
  18.   
  19.     printf("%s\n",src);  
  20.     getchar();  
  21.     return 0;  
  22. }  
  23.   
  24. /* 
  25. *copyright@nciaebupt 转载请注明出处 
  26. *原型:void *memmove(void *dest, const void *src, size_t count); 
  27. *用法:#include <string.h> 
  28. *功能:由src所指内存区域复制count个字节到dest所指内存区域。 
  29. *说明:src和dest所指内存区域可以重叠,但复制后src内容会被更改。函数返回指向dest的指针。 
  30. *自己实现memmove 
  31. * 
  32. */  
  33. #include <cstdio>  
  34. #include <cstring>  
  35.   
  36. void * _memmove(void * dest, const void * src, size_t count)  
  37. {  
  38.     void * ret = dest;  
  39.     if((char *)dest <= (char *)src ||(char *)dest >= ((char *)src) + count)  
  40.     {//如果数据区没有重合,则从低地址向高地址复制  
  41.         while(count--)  
  42.         {  
  43.             *((char *)dest) = *((char *)src);  
  44.             dest = (char *)dest + 1;  
  45.             src = (char *)src + 1;  
  46.         }  
  47.     }  
  48.     else//如果数据区有重合,则从高地址向低地址复制  
  49.     {  
  50.         dest = (char *)dest + count - 1;  
  51.         src = (char *)src + count - 1;  
  52.         while(count--)  
  53.         {  
  54.             *((char *)dest) = *((char *)src);  
  55.             dest = (char *)dest - 1;  
  56.             src = (char *)src -1;  
  57.         }  
  58.     }  
  59.     return ret;  
  60. }  
  61.   
  62. int main(int args,char ** argv)  
  63. {  
  64.     char src[] = "memmove can be very useful......";  
  65.     char dest[40] ={""};  
  66.     _memmove(src + 20,src + 15,11);  
  67.   
  68.     printf("%s\n",src);  
  69.     getchar();  
  70.     return 0;  
  71. }  

C函数库中的memmove实现

     3.3内存比较

      函数原型: void *memcmp( void  const *a , void  const *b ,  size_t   length);

      函数功能:memcmp对两段内存的内容进行比较,这两段内存分别起始于a和b,共比较length个字节。这些值按照五福好字符逐字节比较,函数的返回类型和strcmp函数一样--赋值表示a小于b,正值表示a大于b,零表示相等。由于这些值是根据一串无符号字节进行比较的,所以如果memcmp寒素用于比较不是单字节的数据如浮点型或者这逆行就可能给出不可预知的结果。

  1. /* 
  2. *copyright@nciaebupt 转载请注明出处 
  3. *原型:int memcmp(const void *buf1, const void *buf2, size_t count); 
  4. *用法:#include <string.h> 
  5. *功能:比较内存区域buf1和buf2的前count个字节。 
  6. *说明: 
  7. *   当buf1<buf2时,返回值<0 
  8. *   当buf1=buf2时,返回值=0 
  9. *   当buf1>buf2时,返回值>0 
  10. *使用C函数库中的memcmp 
  11. */  
  12. #include <cstdio>  
  13. #include <cstring>  
  14.   
  15. int main(int args,char ** argv)  
  16. {  
  17.     char str1[] = "Hello World!";  
  18.     char str2[] = "Hello world!";  
  19.   
  20.     int flag = memcmp(str1,str2,sizeof(str1));  
  21.   
  22.     if(flag < 0)    printf("%s is less than %s\n",str1,str2);  
  23.     else if(flag == 0)  printf("%s is equal %s",str1,str2);  
  24.     else    printf("%s is larger than %s",str1,str2);  
  25.   
  26.     getchar();  
  27.     return 0;  
  28.   
  29. }  
  30.   
  31. /* 
  32. *copyright@nciaebupt 转载请注明出处 
  33. *原型:int memcmp(const void *buf1, const void *buf2, size_t count); 
  34. *用法:#include <string.h> 
  35. *功能:比较内存区域buf1和buf2的前count个字节。 
  36. *说明: 
  37. *   当buf1<buf2时,返回值<0 
  38. *   当buf1=buf2时,返回值=0 
  39. *   当buf1>buf2时,返回值>0 
  40. *自己实现memcmp 
  41. */  
  42. #include <cstdio>  
  43.   
  44. int _memcmp(const void * buf1,const void * buf2,size_t count)  
  45. {  
  46.     if(!count)  
  47.         return 0;  
  48.     while(count-- && *((char *)buf1) == *((char *)buf2))  
  49.     {  
  50.         buf1 = (char *)buf1 + 1;  
  51.         buf2 = (char *)buf2 + 1;  
  52.     }  
  53.     return (*((char *)buf1)-*((char *)buf2));  
  54. }  
  55.   
  56. int main(int args,char ** argv)  
  57. {  
  58.     char str1[] = "Hello World!";  
  59.     char str2[] = "Hello world!";  
  60.   
  61.     int flag = _memcmp(str1,str2,sizeof(str1));  
  62.   
  63.     if(flag < 0)    printf("%s is less than %s\n",str1,str2);  
  64.     else if(flag == 0)  printf("%s is equal %s",str1,str2);  
  65.     else    printf("%s is larger than %s",str1,str2);  
  66.   
  67.     getchar();  
  68.     return 0;  
  69.   
  70. }  

C函数库中的memcmp实现

    3.4 内存查询     

     函数原型: void *memchr( void  const  *a ,int ch , size_t  length);

      函数功能:memchr是从a的起始位置开始查找字符ch第一次出现的位置,并返回一个指向该位置的指针,他共查找length个字节。如果这个length个字节中都未找到该字符,函数返回一个NULL指针。

  1. /* 
  2. *copyright@nciaebupt 转载请注明出处 
  3. *原型:void *memchr(const void *buf, int ch, size_t count); 
  4. *用法:#include <string.h> 
  5. *功能:从buf所指内存区域的前count个字节查找字符ch。 
  6. *说明:当第一次遇到字符ch时停止查找。 
  7. *      如果成功,返回指向字符ch的指针;否则返回NULL。 
  8. *使用C函数库中的memchr 
  9. */  
  10. #include <cstdio>  
  11. #include <cstring>  
  12.   
  13. int main(int args,char ** argv)  
  14. {  
  15.     char s[] = "simple string";  
  16.     char *p = (char *)memchr(s,'n',strlen(s));  
  17.   
  18.     if(p != NULL)  
  19.         printf("find the ch in position : %d\n",p - s + 1);  
  20.     else    printf("not find the ch!\n");  
  21.   
  22.     getchar();  
  23.     return 0;  
  24. }  
  25.   
  26. /* 
  27. *copyright@nciaebupt 转载请注明出处 
  28. *原型:void *memchr(const void *buf, int ch, size_t count); 
  29. *用法:#include <string.h> 
  30. *功能:从buf所指内存区域的前count个字节查找字符ch。 
  31. *说明:当第一次遇到字符ch时停止查找。 
  32. *      如果成功,返回指向字符ch的指针;否则返回NULL。 
  33. *自己实现memchr 
  34. */  
  35. #include <cstdio>  
  36.   
  37. void * _memchr(const void *buf,int ch,size_t count)  
  38. {  
  39.     while(count && (*(unsigned char *)buf != (unsigned char )ch))  
  40.     {  
  41.         buf = (unsigned char *)buf + 1;  
  42.         count--;  
  43.     }  
  44.     return (count ? (void *)buf : NULL);  
  45. }  
  46.   
  47. int main(int args,char ** argv)  
  48. {  
  49.     char s[] = "simple string";  
  50.     char *p = (char *)_memchr(s,'n',sizeof(s));  
  51.   
  52.     if(p != NULL)  
  53.         printf("find the ch in position : %d\n",p - s + 1);  
  54.     else    printf("not find the ch!\n");  
  55.   
  56.     getchar();  
  57.     return 0;  
  58. }  

C函数库中的memchr实现

     3.4 内存设置指定字符

      函数原型: void *memset( void  *a , int ch  ,  size_t  length);

       函数功能:memset函数把从a开始的length个字节都设置为字符值ch

      例如

      memset( buffer  , 0 , SIZE);

     把buffer的前SIZE个字节都初始化为0。

      字符串由NUL字节结尾,所以字符串内部不能包含任何NUL字符。但是,非字符串数据内部包含零值得情况并不罕见。你就无法用字符串函数处理这类数据,因为当他门遇到第一个NUL自解释将停止工作。由此产生了以上函数用于处理任意字节序列。


      

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值