1. 程式人生 > >C 標準庫 - string.h之memcpy使用

C 標準庫 - string.h之memcpy使用

uno over character copies www. zhang 別名 .com val

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.
  • 復制的字節數
//
// 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使用