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;
}

文章参考

转载注明出处

C 标准库 - string.h之memcpy使用的更多相关文章

  1. C 标准库 - string.h

    C 标准库 - string.h This header file defines several functions to manipulate C strings and arrays. stri ...

  2. C标准库<string.h>实现

    本文地址:http://www.cnblogs.com/archimedes/p/c-library-string.html,转载请注明源地址. 1.背景知识 <string.h>中声明的 ...

  3. C标准库string.h中几个常用函数的使用详解

    strlen 计算字符串长度 size_t strlen(const char *str) 计算字符串 str 的长度,直到空结束字符,但不包括空结束字符. 函数实现: int Strlen(cons ...

  4. C 标准库 - string.h之memmove使用

    memmove Move block of memory Copies the values of num bytes from the location pointed by source to t ...

  5. C 标准库 - string.h之memcmp使用

    memcmp Compare two blocks of memory. Compares the first num bytes of the block of memory pointed by ...

  6. C 标准库 - string.h之memchr使用

    memchr Locate character in block of memory,Searches within the first num bytes of the block of memor ...

  7. C 标准库 - string.h之strlen使用

    strlen Returns the length of the C string str. The length of a C string is determined by the termina ...

  8. C 标准库 - string.h之strpbrk使用

    strpbrk Locate characters in string,Returns a pointer to the first occurrence in str1 of any of the ...

  9. C 标准库 - string.h之strrchr使用

    strrchr Locate last occurrence of character in string, Returns a pointer to the last occurrence of c ...

随机推荐

  1. How To Make A DFF Read Only Through Form Personalisations? (文档 ID 1289789.1)

    In this Document   Goal   Solution   References APPLIES TO: Oracle Application Object Library - Vers ...

  2. 如何修改TFS 2013中工作项附件大小限制

    默认情况下,TFS工作项的附件大小限制为4MB.我们可以通过调用TFS提供的Web Service将这个限制调整最高到2GB. 调整这个设置的必备条件是你需要拥有TFS应用层管理员的权限.下面来看看如 ...

  3. 使用Eclipse切换TFS工作区

    这个问题首先牵涉到两个概念: - Eclipse的工作区:Eclipse的工作区是运行Eclipse时需要连接的本地代码空间,默认情况下,在Eclipse中创建的项目都保存在Eclipse的工作区中 ...

  4. python怎么生成requirements.txt文件

    生成文件: pip freeze > requirements.txt 安装依赖: pip install -r requirements.txt

  5. Sql语法高级应用之五:使用存储过程实现对明细多层次统计

    前言 前面章节我们讲到了存储过程的基础用法,本章则将一个在项目中实际应用的场景. 在项目中经常会存在这样的需求,例如需要对明细列表进行按组.按级别.按人等进行统计,如果在附带列表的查询条件,又如何实现 ...

  6. Commands that may modify the data set are disabled, because this instance is configured to report errors during writes if RDB snapshotting fails (stop-writes-on-bgsave-error option)

    今天运行Redis时发生错误,错误信息如下: org.springframework.dao.InvalidDataAccessApiUsageException: MISCONF Redis is ...

  7. Express-及中间件的简单理解

    Express Express 是一个基于node平台,保持最小规模的灵活的 Node.js Web 应用程序开发框架,在Node.js基础上扩展对了web应用开发所需要的基础功能为 Web 和移动应 ...

  8. 使用HBuilderX实现打包vue项目成app

    一.准备开发工具 开发工具:HBuilderX 官网地址:http://www.dcloud.io (标准版需要自己安装插件,app开发版已经把app开发常用的插件安装好了,开箱即用,建议使用开发版) ...

  9. angular4套用primeng样式

    首先安装primeng cnpm install primeng --save 这样会在项目目录中增加node_modules\primeng目录 package.json文件增加了以下一行 &quo ...

  10. eNSP 模拟器添加loopback本地回环口

    eNSP只能模拟华为的设备,通常情况下数据通信的传递范围仅限于eNSP中的设备之间. 有时我们在学习更多技术,比如希望将eNSP跟VMware Workstation里的虚拟机互通,或者想让eNSP里 ...