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. SOCK开发之---TCP/IP简介

    在开发通信程序之前,都要先确定这些程序相互通信所使用的协议(protocol),在深入设计前,我们都需要先从高层次来判断通信由哪个程序发起以及相应在何时产生. 举例来说,一般认为web服务器是一个长时 ...

  2. Default style sheet for HTML 4

    http://www.w3.org/TR/CSS21/sample.html html, address, blockquote, body, dd, div, dl, dt, fieldset, f ...

  3. easyui的filebox过滤文件

    示例:<input class="easyui-filebox" data-options="buttonText:'选择文件',accept:'image/gif ...

  4. Linux CentOS环境下.Net Core SDK安装

    1.安装.Net SDK 在安装.NET之前,需要注册产品存储库并安装所需的依赖关系. 打开Linux命令提示符并运行以下命令: sudo rpm -Uvh https://packages.micr ...

  5. C#基础笔记(第二十天)

    1.复习属性:保护字段的构造函数:初始化对象初始化对象:给对象的每个属性去赋值什么时候会调用构造函数:当我们new的时候面向对象中需要注意的两个关键字this 1.代表当前类的对象 2.调用自己的构造 ...

  6. Python读取mdb文件以及shell检测

    最近写了两个python的脚本不过实际意义不是很大,就是想练练python写程序,一直研究web方面脚本写的少多了,还有C语言也用的少多了.现在有时间得多写写程序,别把以前学到的知识给忘了. 作者: ...

  7. CSRF漏洞原理说明与利用方法

    翻译者:Fireweed 原文链接:http://seclab.stanford.edu/websec/ 一 .什么是CSRF Cross-Site Request Forgery(CSRF),中文一 ...

  8. 学习xss模拟构造攻击(第一篇)

    本文作者:i春秋签约作家——rosectow 0×00前言 XSS又名叫CSS全程(cross site scriptting),中文名跨站脚本攻击,目前网站的常见漏洞之一,它的危害没有像上传漏洞,s ...

  9. 爬虫实战4:用selenium爬取淘宝美食

    方案1:一次性爬取全部淘宝美食信息 1. spider.py文件如下 __author__ = 'Administrator' from selenium import webdriver from ...

  10. spring cloud ribbon源码解析(一)

    我们知道spring cloud中restTemplate可以通过服务名调接口,加入@loadBalanced标签就实现了负载均衡的功能,那么spring cloud内部是如何实现的呢? 通过@loa ...