博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C语言字符串常用函数学习(一)
阅读量:7234 次
发布时间:2019-06-29

本文共 2590 字,大约阅读时间需要 8 分钟。

  hot3.png

memcpy

void * memcpy ( void * destination, const void * source, size_t num );

复制内存

从源source所指内存地址的起始位置开始,复制num个字节到目标destination所指内存地址的起始位置中。

源和目标指针所指向的对象的基本类型是不相关的,结果是数据的二进制副本。

该函数不检查源字符串中的终止符,总是按照num数来复制字节。

为了避免溢出,sourcedestination数组的长度应至少num字节,而且不应重叠(如果重叠的内存块,memmove是一个更安全的方法)。

示例

/* memcpy example */#include 
#include 
struct{    char name[40];    int age;} person, person_copy;int main (){    char myname[] ="Pierre de Fermat";    /* using memcpy to copy string: */    memcpy (person.name, myname, strlen(myname)+1 );    person.age = 46;        /* using memcpy to copy structure: */    memcpy ( &person_copy, &person,sizeof(person) );        printf ("person_copy: %s, %d \n", person_copy.name, person_copy.age );    return0;}

执行结果:

person_copy: Pierre de Fermat, 46

memmove

void * memmove ( void * destination, const void * source, size_t num );

移动内存块

source指向的内存位置开始的num字节复制destination指向的内存位置复制发生在中间缓冲区,允许destinationsource重叠。

sourcedestination指针所指向对象的基本类型是不相关的,其结果是数据的二进制副本。

该函数不检查源字符串中的终止符,总是按照num数来复制字节。

为了避免溢出,sourcedestination数组的长度应至少num字节。。

示例

/* memmove example */#include 
#include 
int main (){    char str[] ="memmove can be very useful......";    memmove(str+20,str+15,11);    puts(str);        return0;}

执行结果:

memmove can be very very useful.

strcpy

char * strcpy ( char * destination, const char * source );

复制字符串

复制source指向的字符串到destination指的字符串,包括终止空字符(并在该点停止)。

为了避免溢出,destination数组的大小必须足够长,以容纳source字符串(包括终止空字符),并且在内存上与source不重叠。

示例

/* strcpy example */#include 
#include 
int main (){  char str1[]="Sample string";  char str2[40];  char str3[40];  strcpy (str2,str1);  strcpy (str3,"copy successful");  printf ("str1: %s\nstr2: %s\nstr3: %s\n",str1,str2,str3);  return 0;}

执行结果:

str1: Sample stringstr2: Sample stringstr3: copy successful

strncpy

char * strncpy ( char * dest, const char * source, size_t num );

复制字符串字符

复制source字符串的前num个字符到dest字符串,如果source字符串长度小于num,剩下的字节用0x00填充。

如果source字符串长度大于num,dest字符串的末尾就是非空字符,在这种情况下,dest不应被视为空终止字符串,否则读取时会溢出。

sourcedest不应有重叠。

示例

/* strncpy example */#include 
#include 
int main (){  char str1[]= "To be or not to be";  char str2[40];  char str3[40];  /* copy to sized buffer (overflow safe): */  strncpy ( str2, str1, sizeof(str2) );  /* partial copy (only 5 chars): */  strncpy ( str3, str2, 5 );  str3[5] = '\0';   /* null character manually added */  puts (str1);  puts (str2);  puts (str3);  return 0;}

执行结果:

To be or not to beTo be or not to beTo be

转载于:https://my.oschina.net/raygo/blog/543087

你可能感兴趣的文章
设置导航栏的背景颜色的分类
查看>>
Spring-AOP实践 - 统计访问时间--StopWatch
查看>>
Atitit.事件机制 与 消息机制的联系与区别
查看>>
VS2013安装部署项目
查看>>
微信支付官方.net版之坑你没商量
查看>>
iOS -- 生成有logo的二维码
查看>>
最全面的百度地图JavaScript离线版开发
查看>>
tracert-命令小结
查看>>
爱上MVC~AuthorizeAttribute验证不通过如何停止当前上下文
查看>>
备忘录模式
查看>>
支付宝支付功能开发简易流程
查看>>
【服务器环境搭建-Centos】常用系统命令篇
查看>>
Swift用UIBezierPath来画圆角矩形、自定义多路径图形
查看>>
delphi FMX 数字下拉滑动
查看>>
重磅榜单!互联网金融Top100总估值超1.1万亿,27家独角兽上榜!
查看>>
cocos2d-x wp8 中文显示问题
查看>>
数字证书学习笔记
查看>>
Linux 文件与目录管理
查看>>
sublime3 docblocker插件定制自己的注释,配置步骤
查看>>
【Java并发编程】15、ReentrantLock实现原理深入探究
查看>>