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 ...
随机推荐
- Ubuntu下Jenkins(docker)配置Docker远程启动
背景: 在做用Jenkins构建docker的操作,需要用Jenkins调用docker命令,需要先安装docker-build-step插件,然后开启docker远程访问. 默认情况下,Docker ...
- SparkSQL大数据实战:揭开Join的神秘面纱
本文来自 网易云社区 . Join操作是数据库和大数据计算中的高级特性,大多数场景都需要进行复杂的Join操作,本文从原理层面介绍了SparkSQL支持的常见Join算法及其适用场景. Join背景介 ...
- Android 如何保存资源 Id 数组在 res/values/arrays.xml 里
<resources> <!-- Tracks Information --> <array name="music_ids"> <ite ...
- 201621123023《Java程序设计》第8周学习总结
一.本周学习总结 二.书面作业 1. ArrayList代码分析 1.1 解释ArrayList的contains源代码 由图可知,传入参数后调用indexOf函数来判断是否存在,会循环整个eleme ...
- Remoteland HDU - 4196
题意: 给出一个n,在[1, n] 中挑选几个不同的数相乘,求能的到的最大完全平方数 解析: 最大的肯定是n!, 然后n!不一定是完全平方数 (我们知道一个完全平方数,质因子分解后,所有质因子的质数均 ...
- Lora通讯
Lora通讯 今年放弃了电源,踏入了物联网行业,也不知道算不算放弃吧,但我内心始终在呐喊,早晚会把你拿下,现在暂且放过你! 首先普及一下物联网,物联网是21世纪兴起的行业,最开始是由比尔盖茨在1995 ...
- 【12c OCP】CUUG OCP认证071考试原题解析(33)
33.choose the best answer View the Exhibit and examine the structure of the ORDER_ITEMS table. Exami ...
- “全栈2019”Java多线程第十二章:后台线程setDaemon()方法详解
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...
- Linux Kill 无法关闭进程
Kill -signal Process# signal 表示kill命令给进程发送的信号 Kill命令实际上执行的动作,是给进程发送信号,常用: INT 2 这个就是你在bash下面用Ctrl+C ...
- 洛谷P1742 最小圆覆盖(计算几何)
题面 传送门 题解 之前只是在抄题解--这篇才算是真正自己想的吧-- 首先我们把输入序列给\(random\)一下防止出题人好心送你一个毒瘤序列 我们设\(r\)为当前最大半径,\(o\)为此时对应圆 ...