C 标准库 - string.h之memmove使用
memmove
- Move block of memory
- Copies the values of num bytes from the location pointed by source to the memory block pointed by destination. Copying takes place as if an intermediate buffer were used, allowing the destination and source to overlap.
- The underlying type of the objects pointed by both the source and destination pointers are irrelevant for this function; The result is a binary copy of the data.
- The function does not check for any terminating null character in source - it always copies exactly num bytes.
- To avoid overflows, the size of the arrays pointed by both the destination and source parameters, shall be at least num bytes.
- 从 source 所指向的对象复制 num 个字节到 destination 所指向的对象。两个对象都被转译成 unsigned char 的数组。对象可以重叠:如同复制字符到临时数组,再从该数组到 destination 一般发生复制。
- 从 source 复制 num 个字符到 destination,但是在重叠内存块这方面,memmove() 是比 memcpy() 更安全的方法。如果目标区域和源区域有重叠的话,memmove() 能够保证源串在被覆盖之前将重叠区域的字节拷贝到目标区域中,复制后源区域的内容会被更改。如果目标区域与源区域没有重叠,则和 memcpy() 函数功能相同。
- 若出现 destination 数组末尾后的访问则行为未定义。
- 若 destination 或 source 为空指针则行为未定义。
void * memmove ( void * destination, const void * source, size_t num );
Parameters
destination
- Pointer to the destination array where the content is to be copied, type-casted to a pointer of type void*.
- 指向用于存储复制内容的目标数组,类型强制转换为 void* 指针。
- 指向复制目的对象的指针
source
- Pointer to the source of data to be copied, type-casted to a pointer of type const void*.
- 指向要复制的数据源,类型强制转换为 void* 指针。
- 指向复制来源对象的指针
num
- Number of bytes to copy.size_t is an unsigned integral type.
- 要被复制的字节数。
- 要复制的字节数
Return Value
- destination is returned.
- 该函数返回一个指向目标存储区 destination 的指针。
- 返回 destination 的副本,本质为更底层操作的临时内存地址,在实际操作中不建议直接使用此地址,操作完成以后,真正有意义的地址是destination本身。
Example
//
// Created by zhangrongxiang on 2018/2/10.
//
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
char str[32] = "I am your GOD";
char str2[32] = "Hello World";
char str3[] = "void * memmove ( void * destination, const void * source, size_t num );";
memmove(str2, str, strlen(str));
printf("%s\n", str2);//I am your GOD
memmove(str2, "hi\0hi", sizeof(str2));
printf("%s\n", str2);//hi
printf("%c%c%c%c\n", str2[3], str2[4], str2[5], str2[6]);//hi\0\0
memmove(str, str3, sizeof(str) - 1);
for (int i = 0; i < sizeof(str); ++i) {
printf("%c", str[i]);
}//void * memmove ( void * destina%
/////////////////////////////////////////////////////////////////////
char str4[] = "1234567890";
puts(str4);//1234567890
memmove(str4 + 4, str4 + 3, 3); // 从 [4,5,6] 复制到 [5,6,7]
puts(str4);//1234456890
////////////////////////////////////////////////////////////////////
// 设置分配的内存的有效类型为 int
int *p = malloc(3 * sizeof(int)); // 分配的内存无有效类型
int arr[3] = {1, 2, 3};
memmove(p, arr, 3 * sizeof(int)); // 分配的内存现在拥有有效类型
printf("%d%d%d\n", p[0], p[1], p[2]);//123
printf("%d%d%d\n", *p, *(p + 1), *(p + 2));//123
return 0;
}
文章参考
- http://zh.cppreference.com/w/c/string/byte/memmove
- http://www.cplusplus.com/reference/cstring/memmove/
- http://www.runoob.com/cprogramming/c-function-memmove.html
转载注明出处
C 标准库 - string.h之memmove使用的更多相关文章
- C 标准库 - string.h
C 标准库 - string.h This header file defines several functions to manipulate C strings and arrays. stri ...
- C标准库<string.h>实现
本文地址:http://www.cnblogs.com/archimedes/p/c-library-string.html,转载请注明源地址. 1.背景知识 <string.h>中声明的 ...
- C 标准库 - string.h之memcpy使用
memcpy Copy block of memory Copies the values of num bytes from the location pointed to by source di ...
- C标准库string.h中几个常用函数的使用详解
strlen 计算字符串长度 size_t strlen(const char *str) 计算字符串 str 的长度,直到空结束字符,但不包括空结束字符. 函数实现: int Strlen(cons ...
- C 标准库 - string.h之memcmp使用
memcmp Compare two blocks of memory. Compares the first num bytes of the block of memory pointed by ...
- C 标准库 - string.h之memchr使用
memchr Locate character in block of memory,Searches within the first num bytes of the block of memor ...
- C 标准库 - string.h之strlen使用
strlen Returns the length of the C string str. The length of a C string is determined by the termina ...
- C 标准库 - string.h之strpbrk使用
strpbrk Locate characters in string,Returns a pointer to the first occurrence in str1 of any of the ...
- C 标准库 - string.h之strrchr使用
strrchr Locate last occurrence of character in string, Returns a pointer to the last occurrence of c ...
随机推荐
- Windows 以及 Xcode下编译调试 libcurl 源码
curl 这个工具大家都很熟悉. 前几天因为要跟踪curl的实现细节, 不得不设法搭建curl的调试工程. 我们分别在windows visual studio 和 mac 上的 xcode 下搭建调 ...
- 连接池--sp_reset_connection
--当客户端使用连接池访问数据库时,客户端使用OPEN来重用数据库连接,使用CLOSE来断开数据库连接,但并不物理上新建和断开连接,因此可以提高程序运行速度并降低性能损耗. --ADO和ADO.NET ...
- 创建jdk8基础镜像
https://blog.csdn.net/qq_35981283/article/details/80738451
- C#——Socket
最近公司让我搞个socket小程序(服务端). 主要的功能:客户端发字符串到服务端,服务端接收后在界面上显示. 参照了网上许多代码,自己从中修改整理代码. public Form4() { Initi ...
- WP8.1StoreApp(WP8.1RT)---第三方启动
8.1的协议和wp8是相互通用的 被启动: 相比较wp8而言,基本变化不大,但添加方式更直观了 1:打开Package.appxmanifest 2:切换到"声明"选项卡 3:左侧 ...
- Promise之你看得懂的Promise
本文由作者陈旭锋(任职网易考拉)授权网易云社区发布. Promise源码详解 学习知识要善于思考,思考,再思考. -- 爱因斯坦 1.回调地狱 曾几何时,我们的代码是这样的,为了拿到回调的结果,不得不 ...
- 【12c OCP】最新CUUG OCP-071考试题库(51题)
------------------------------------------------------- 51.(12-10)choose the best answer: Evaluate t ...
- “全栈2019”Java异常第二十二章:try-with-resources语句详解
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java异 ...
- 图的最小生成树的理解和实现:Prim和Kruskal算法
最小生成树 一个连通图的生成树是一个极小的连通子图,它含有图中所有的顶点,但只有足以构成一棵树的n-1条边.我们将构造连通网的最小代价生成树称为最小生成树(Minimum Cost Spanning ...
- 阿里云服务器18个数据中心测试IP地址以及测试方法
我们用户在选择阿里云服务器的时候是不是感觉阿里云的数据中心太多太多,确实阿里云服务器机房是有很多,国外国外机房大约有18个,甚至更多,因为还在不断的增加机房.对于商家而言增加不同的机房可以满足不同的项 ...