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. [label][JavaScript]闭包阅读笔记

    原文链接来源:                       http://www.ruanyifeng.com/blog/2009/08/learning_javascript_closures.ht ...

  2. mysql in语句在java中的使用

    做权限的时候,通过role角色中的roleid,在auth权限中查找角色对应的权限. sql语句: SELECT authName from auth where authId in (1,2,3,4 ...

  3. WPF自定义滚动条

    我修改了一些地方,部分代码参考了博主 https://www.cnblogs.com/xiaomingg/ <!-- ScrollViewer 滚动条 --> <Style x:Ke ...

  4. [uwp]自定义图形裁切控件

    开始之前,先上一张美图.图中的花叫什么,我已经忘了,或者说从来就不知道,总之谓之曰“野花”.只记得花很美,很香,春夏时节,漫山遍野全是她.这大概是七八年前的记忆了,不过她依旧会很准时的在山上沐浴春光, ...

  5. Cannot modify header information问题的解决方法【新浪云经常遇到的错误】

    我做了一个统一的出错提示函数,在函数执行里面,先处理出错的地址写入cookie以方便用户登陆以后可以直接跳转到要执行的这个页面,可是发现在服务器上测试时,竟然提示本地没有出现的错误: Warning: ...

  6. CTF之信息泄漏

    web源码泄漏 .hg源码泄漏: 漏洞成因:hg  init的时候会生成.hg,http://www.xx.com/.hg/, 工具:dvcs-ripper,(rip-hg.pl -v -u http ...

  7. django 结合 OPTIONS方法 处理跨域请求(单个视图方法中)

    OPTIONS 方法比较少见,该方法用于请求服务器告知其支持哪些其他的功能和方法.通过 OPTIONS 方法,可以询问服务器具体支持哪些方法,或者服务器会使用什么样的方法来处理一些特殊资源.可以说这是 ...

  8. git 使用merge 对本地分支进行合并 并进行代码提交的流程

    1.只有当将修改内容commit后 该修改才完全生效,进行merge前需要将两个分支修改的内容都进行commit 2.假设本地两个分支   用于开发的分支:dev    用于同步远程仓库的分支:mas ...

  9. HTTP报文语法/HTTP组成

        一.HTTP报文分类:请求报文和响应报文 请求报文会向Web服务器请求一个动作,响应报文会将请求的结果返回给客户端 请求报文格式: <method>  <request-UR ...

  10. 【git】——简单用法

    git 更新远程代码到本地 git fetch origin master git merge origin/master 冲突: Your local changes to the followin ...