内存和字符串.docx

上传人:b****2 文档编号:20041637 上传时间:2023-04-24 格式:DOCX 页数:17 大小:17.62KB
下载 相关 举报
内存和字符串.docx_第1页
第1页 / 共17页
内存和字符串.docx_第2页
第2页 / 共17页
内存和字符串.docx_第3页
第3页 / 共17页
内存和字符串.docx_第4页
第4页 / 共17页
内存和字符串.docx_第5页
第5页 / 共17页
点击查看更多>>
下载资源
资源描述

内存和字符串.docx

《内存和字符串.docx》由会员分享,可在线阅读,更多相关《内存和字符串.docx(17页珍藏版)》请在冰豆网上搜索。

内存和字符串.docx

内存和字符串

1、memcpy

void*memcpy(void*destination,constvoid*source,size_tnum);

复制内存块

从source指向的位置直接复制num个字节的值到destination指向的内存块。

对于这个函数,source和destination指向的对象的底层类型是无关的;结果是数据的一个二进制复制。

这个函数不检查任何结束空字符(null)——它总是准确地复制num个字节。

为了避免溢出,source和destination两个参数指向的数组的大小应该至少是num字节,并且不应该重叠(对于重叠内存块,memmove是一个更安全的方法))。

参数

destination

      指向内容被复制到的目的数组的指针,类型被转换为void*。

source

      指向复制数据的源数组的指针,类型被转换为void*。

num

      要复制的字节数。

返回值

      返回目的指针destination。

2、memmove

void*memmove(void*destination,constvoid*source,size_tnum);

移动内存块

从source指向的位置复制num个字节的值到destination指向的内存块。

复制操作的发生就好像使用了中间缓存,允许destination和source重叠。

对于这个函数,source和destination指向的对象的底层类型是无关的;结果是数据的一个二进制复制。

这个函数不检查任何结束空字符(null)——它总是准确地复制num个字节。

为了避免溢出,source和destination两个参数指向的数组的大小应该至少是num字节。

参数

destination

      指向内容被复制到的目的数组的指针,类型被转换为void*。

source

      指向复制数据的源数组的指针,类型被转换为void*。

num

      要复制的字节数。

返回值

      返回目的指针destination。

3、memset

void*memset(void*ptr,intvalue,size_tnum);

填充内存块

将ptr指向的没存块的前num个字节设置为特定的值value(被解释为一个unsignedchar类型的值)。

参数

ptr

      指向需要填充的内存块的指针。

value

      要设置的值,这个值是作为int型传递的,但是该函数用这个值的unsignedchar类型转换值来填充内存块。

num

      要是指特定值的字节数。

返回值

返回指针ptr。

4、memchr

constvoid*memchr(constvoid*ptr,intvalue,size_tnum);

void*memchr(void*ptr,intvalue,size_tnum);

在内存块中定位字符

在ptr指向的没存块的前num个字节中搜索value值得第一次出现(被解释为一个unsignedchar类型的值),并返回指向该值的指针。

参数

ptr

      指向需要被执行搜索的内存块的指针。

value

      需要被定位的值。

这个值是作为int型传递的,但是该函数用这个值的unsignedchar类型转换值来进行逐位的搜索。

num

      要是分析的字节数。

返回值

      返回指向ptr指向的内存块中value值第一次出现位置的指针。

      如果没有找到value值,则该函数返回NULL。

5、memcmp

intmemcmp(constvoid*ptr1,constvoid*ptr2,size_tnum);

比较两个内存块

将ptr2指向的内存块的签num个字节与ptr2指向的内存块的前num个字节进行比较,如果它们都相符合则返回0,反之则返回一个指示其中一个较大的非零值。

参数

ptr1

      指向内存块的指针。

ptr2

      指向内存块的指针

num

      需要比较的字节数。

返回值

返回一个指示两个内存块的内容间的关系的整型值。

0值表示两个内存块的内容是相等的。

一个大于0的值表示,两个内存块中第一个不匹配的字节是ptr1的比ptr2的大,作为unsignedchar类型比较的;小于0则表示相反。

函数名:

stpcpy

功能:

拷贝一个字符串到另一个

用法:

char*stpcpy(char*destin,char*source);

程序例:

#include

#include

intmain(void)

{

  charstring[10];

  char*str1="abcdefghi";

  stpcpy(string,str1);

  printf("%s\n",string);

  return0;

}

 

函数名:

strcat

功能:

字符串拼接函数

用法:

char*strcat(char*destin,char*source);

程序例:

#include

#include

intmain(void)

{

  chardestination[25];

  char*blank="",*c="C++",*Borland="Borland";

  strcpy(destination,Borland);

  strcat(destination,blank);

  strcat(destination,c);

  printf("%s\n",destination);

  return0;

}

 

函数名:

strchr

功能:

在一个串中查找给定字符的第一个匹配之处\

用法:

char*strchr(char*str,charc);

程序例:

#include

#include

intmain(void)

{

   charstring[15];

   char*ptr,c='r';

   strcpy(string,"Thisisastring");

   ptr=strchr(string,c);

   if(ptr)

      printf("Thecharacter%cisatposition:

%d\n",c,ptr-string);

   else

      printf("Thecharacterwasnotfound\n");

   return0;

}

 

函数名:

strcmp

功能:

串比较

用法:

intstrcmp(char*str1,char*str2);

看Asic码,str1>str2,返回值>0;两串相等,返回0

程序例:

#include

#include

intmain(void)

{

   char*buf1="aaa",*buf2="bbb",*buf3="ccc";

   intptr;

   ptr=strcmp(buf2,buf1);

   if(ptr>0)

      printf("buffer2isgreaterthanbuffer1\n");

   else

      printf("buffer2islessthanbuffer1\n");

   ptr=strcmp(buf2,buf3);

   if(ptr>0)

      printf("buffer2isgreaterthanbuffer3\n");

   else

      printf("buffer2islessthanbuffer3\n");

   return0;

}

 

函数名:

strncmpi

功能:

将一个串中的一部分与另一个串比较,不管大小写

用法:

intstrncmpi(char*str1,char*str2,unsignedmaxlen);

程序例:

#include

#include

intmain(void)

{

  char*buf1="BBB",*buf2="bbb";

  intptr;

  ptr=strcmpi(buf2,buf1);

  if(ptr>0)

     printf("buffer2isgreaterthanbuffer1\n");

  if(ptr<0)

     printf("buffer2islessthanbuffer1\n");

  if(ptr==0)

     printf("buffer2equalsbuffer1\n");

  return0;

}

 

函数名:

strcpy

功能:

串拷贝

用法:

char*strcpy(char*str1,char*str2);

程序例:

#include

#include

intmain(void)

{

   charstring[10];

   char*str1="abcdefghi";

   strcpy(string,str1);

   printf("%s\n",string);

   return0;

}

 

函数名:

strcspn

功能:

在串中查找第一个给定字符集内容的段

用法:

intstrcspn(char*str1,char*str2);

程序例:

#include

#include

#include

intmain(void)

{

   char*string1="1234567890";

   char*string2="747DC8";

   intlength;

   length=strcspn(string1,string2);

   printf("Characterwherestringsintersectisatposition%d\n",length);

   return0;

}

 

函数名:

strdup

功能:

将串拷贝到新建的位置处

用法:

char*strdup(char*str);

程序例:

#include

#include

#include

intmain(void)

{

   char*dup_str,*string="abcde";

   dup_str=strdup(string);

   printf("%s\n",dup_str);

   free(dup_str);

   return0;

}

 

函数名:

stricmp

功能:

以大小写不敏感方式比较两个串

用法:

intstricmp(char*str1,char*str2);

程序例:

#include

#include

intmain(void)

{

  char*buf1="BBB",*buf2="bbb";

  intptr;

  ptr=stricmp(buf2,buf1);

  if(ptr>0)

     printf("buffer2isgreaterthanbuffer1\n");

  if(ptr<0)

     printf("buffer2islessthanbuffer1\n");

  if(ptr==0)

     printf("buffer2equalsbuffer1\n");

  return0;

}

 

函数名:

strerror

功能:

返回指向错误信息字符串的指针

用法:

char*strerror(interrnum);

程序例:

#include

#include

intmain(void)

{

  char*buffer;

  buffer=strerror(errno);

  printf("Error:

%s\n",buffer);

  return0;

}

 

函数名:

strcmpi

功能:

将一个串与另一个比较,不管大小写

用法:

intstrcmpi(char*str1,char*str2);

程序例:

#include

#include

intmain(void)

{

  char*buf1="BBB",*buf2="bbb";

  intptr;

  ptr=strcmpi(buf2,buf1);

  if(ptr>0)

     printf("buffer2isgreaterthanbuffer1\n");

  if(ptr<0)

     printf("buffer2islessthanbuffer1\n");

  if(ptr==0)

     printf("buffer2equalsbuffer1\n");

  return0;

}

 

函数名:

strncmp

功能:

串比较

用法:

intstrncmp(char*str1,char*str2,intmaxlen);

程序例:

#include

#include

intmain(void)

{

  char*buf1="aaabbb",*buf2="bbbccc",*buf3="ccc";

  intptr;

  ptr=strncmp(buf2,buf1,3);

  if(ptr>0)

     printf("buffer2isgreaterthanbuffer1\n");

  else

     printf("buffer2islessthanbuffer1\n");

  ptr=strncmp(buf2,buf3,3);

  if(ptr>0)

     printf("buffer2isgreaterthanbuffer3\n");

  else

     printf("buffer2islessthanbuffer3\n");

  return(0);

}

 

函数名:

strncmpi

功能:

把串中的一部分与另一串中的一部分比较,不管大小写

用法:

intstrncmpi(char*str1,char*str2);

程序例:

#include

#include

intmain(void)

{

  char*buf1="BBBccc",*buf2="bbbccc";

  intptr;

  ptr=strncmpi(buf2,buf1,3);

  if(ptr>0)

     printf("buffer2isgreaterthanbuffer1\n");

  if(ptr<0)

     printf("buffer2islessthanbuffer1\n");

  if(ptr==0)

     printf("buffer2equalsbuffer1\n");

  return0;

}

 

函数名:

strncpy

功能:

串拷贝

用法:

char*strncpy(char*destin,char*source,intmaxlen);

程序例:

#include

#include

intmain(void)

{

  charstring[10];

  char*str1="abcdefghi";

  strncpy(string,str1,3);

  string[3]='\0';

  printf("%s\n",string);

  return0;

}

 

函数名:

strnicmp

功能:

不注重大小写地比较两个串

用法:

intstrnicmp(char*str1,char*str2,unsignedmaxlen);

程序例:

#include

#include

intmain(void)

{

  char*buf1="BBBccc",*buf2="bbbccc";

  intptr;

  ptr=strnicmp(buf2,buf1,3);

  if(ptr>0)

     printf("buffer2isgreaterthanbuffer1\n");

  if(ptr<0)

     printf("buffer2islessthanbuffer1\n");

  if(ptr==0)

     printf("buffer2equalsbuffer1\n");

  return0;

}

 

函数名:

strnset

功能:

将一个串中的所有字符都设为指定字符

用法:

char*strnset(char*str,charch,unsignedn);

程序例:

#include

#include

intmain(void)

{

  char*string="abcdefghijklmnopqrstuvwxyz";

  charletter='x';

  printf("stringbeforestrnset:

%s\n",string);

  strnset(string,letter,13);

  printf("stringafterstrnset:

%s\n",string);

  return0;

}

 

函数名:

strpbrk

功能:

在串中查找给定字符集中的字符

用法:

char*strpbrk(char*str1,char*str2);

程序例:

#include

#include

intmain(void)

{

  char*string1="abcdefghijklmnopqrstuvwxyz";

  char*string2="onm";

  char*ptr;

  ptr=strpbrk(string1,string2);

  if(ptr)

     printf("strpbrkfoundfirstcharacter:

%c\n",*ptr);

  else

     printf("strpbrkdidn'tfindcharacterinset\n");

  return0;

}

 

函数名:

strrchr

功能:

在串中查找指定字符的最后一个出现

用法:

char*strrchr(char*str,charc);

程序例:

#include

#include

intmain(void)

{

  charstring[15];

  char*ptr,c='r';

  strcpy(string,"Thisisastring");

  ptr=strrchr(string,c);

  if(ptr)

     printf("Thecharacter%cisatposition:

%d\n",c,ptr-string);

  else

     printf("Thecharacterwasnotfound\n");

  return0;

}

 

函数名:

strrev

功能:

串倒转

用法:

char*strrev(char*str);

程序例:

#include

#include

intmain(void)

{

  char*forward="string";

  printf("Beforestrrev():

%s\n",forward);

  strrev(forward);

  printf("Afterstrrev():

%s\n",forward);

  return0;

}

 

函数名:

strset

功能:

将一个串中的所有字符都设为指定字符

用法:

char*strset(char*str,charc);

程序例:

#include

#include

intmain(void)

{

  charstring[10]="123456789";

  charsymbol='c';

  printf("Beforestrset():

%s\n",string);

  strset(string,symbol);

  printf("Afterstrset():

%s\n",string);

  return0;

}

 

函数名:

strspn

功能:

在串中查找指定字符集的子集的第一次出现

用法:

intstrspn(char*str1,char*str2);

程序例:

#include

#include

#include

intmain(void)

{

  char*string1="1234567890";

  char*st

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 人文社科 > 视频讲堂

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1