C 标准库 - string.h之memcpy使用
memcpy
- Copy block of memory
- Copies the values of num bytes from the location pointed to by source directly to the memory block pointed to by destination.
- The underlying type of the objects pointed to 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 to by both the destination and source parameters, shall be at least num bytes, and should not overlap (for overlapping memory blocks, memmove is a safer approach).
- 从存储区 source 复制 num 个字符到存储区 destination
- 从 source 所指向的对象复制 num 个字符到 destination 所指向的对象。两个对象都被转译成 unsigned char 的数组。
- 若访问发生在 destination 数组结尾后则行为未定义。
void * memcpy ( 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.
- 复制的字节数
Example
//
// Created by zhangrongxiang on 2018/2/9 10:32
// File memcpy
//
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
#include <string.h>
#include <stdlib.h>
struct {
char name[40];
int age;
} person, person_copy;
//C 库函数 void *memcpy(void *str1, const void *str2, size_t n) 从存储区 str2 复制 n 个字符到存储区 str1。
int main() {
int i = 0;
// 简单用法
char source[] = "once upon a midnight dreary...", dest[4];
memcpy(dest, source, sizeof dest);
//printf("%s\n", dest);//未定义,不含\0
//printf("length -> %d\n", (int) strlen(dest));
for (i = 0; i < sizeof dest; ++i)
putchar(dest[i]);
printf("\n");
// 设置分配的内存的有效类型为 int
int *p = malloc(3 * sizeof(int)); // 分配的内存无有效类型
int arr[3] = {5, 2, 3};
memcpy(p, arr, 3 * sizeof(int)); // 分配的内存现在拥有有效类型
for (i = 0; i < 3; ++i) {
printf("%d == %d \n", *(p + i), p[i]);
}
///////////////////////////////////////////////////////////////////////////////////////////
// reinterpreting data
double d = 0.1;
// int64_t n = *(int64_t*)(&d); // 严格别名使用违规
int64_t n;
memcpy(&n, &d, sizeof d); // OK
printf("\n%a is %" PRIx64 " as an int64_t\n", d, n);
////////////////////////////////////////////////////////////////////////////////////////////
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);
return 0;
}
文章参考
- http://www.cplusplus.com/reference/cstring/memcpy/
- http://zh.cppreference.com/w/c/string/byte/memcpy
- http://www.runoob.com/cprogramming/c-function-memcpy.html
转载注明出处
C 标准库 - string.h之memcpy使用的更多相关文章
- 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中几个常用函数的使用详解
strlen 计算字符串长度 size_t strlen(const char *str) 计算字符串 str 的长度,直到空结束字符,但不包括空结束字符. 函数实现: int Strlen(cons ...
- C 标准库 - string.h之memmove使用
memmove Move block of memory Copies the values of num bytes from the location pointed by source to t ...
- 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 ...
随机推荐
- 【树状DP】星象仪
题目描述 在寂寞的夜里,星象仪是非常浪漫的东西.但是,你作为一个精神稍微有点不太正常的Geek,把原本正常的星象仪改造得像电报发送器一样.当然,你这个的构造还要更加奇葩一点.具体来说,你的星象仪是一棵 ...
- Github的注册经历
姓名 韦军 学号 1413042023 班级 网络141 兴趣爱好 读书 上网 在注册Github时,先去网上下载了一个Github的app,打开一看全是英文,还是看懂了一些,点击开始注册,在注册时还 ...
- 学习python的第五天
4.30自我总结 一复习 1.查看数据类型 #数值10的位置 print(di(10)) #数值10的样式 print(type(10)) 2.关于变量的一些补充 a=1 b=1 c=1 #a,b,c ...
- 在SharePoint列表中使用动态筛选条件[今日][Today]
如果在SharePoint使用了日历控件或者其他列表中有时间字段,用户经常希望能够动态使用条件字段进行筛选,例如希望筛选出开始日期是今天的事件.未来三日的事件. SharePoint的列表筛选条件支持 ...
- VUE 学习笔记 一 指令
1.声明式渲染 v-bind 特性被称为指令.指令带有前缀 v-,以表示它们是 Vue 提供的特殊特性 <div id='app'> <span v-bind:title=" ...
- ie11下ajax用escape发送中文参数失败
一个项目中,登录请求是ajax,get模式.登录名无中文可以正常登录:登录名是中文则偶尔可以登录,大部分情况下无法登录,ajax请求无法发送成功. 登录名是用js的escape函数转码. 经过多次测试 ...
- driver.get()和driver.navigate().to()到底有什么不同?-----Selenium快速入门(四)
大家都知道,这两个方法都是跳转到指定的url地址,那么这两个方法有什么不同呢?遇到这种情况,第一反应就是查查官方的文档. 官方文档的说法是:Load a new web page in the cur ...
- WPF自定义进度条
<!--进度条 4812--> <LinearGradientBrush x:Key="ProgressBarIndicatorAnimatedFill" Sta ...
- 管道/FIFO注意事项
管道 1. 其本质是一个伪文件(实为内核缓冲区) 2. 由两个文件描述符引用,一个表示读端,一个表示写端. 3. 规定数据从管道的写端流入管道,从读端流出. 管道的原理: 管道实为内核使用环形队列机制 ...
- python中的内置函数(一), lambda, filter, map
https://www.processon.com/view/link/5c10da0ce4b099ae3e137bf6 1.内置函数 内置函数就是python中提供的,可以直接拿来用的函数,比如pr ...