C/C++ 中 memcpy() 函式
memcpy() 函式用於將一個記憶體塊從一個位置複製到另一個位置。一個是源,另一個是目標,由指標指向。這在 C 語言的“string.h”標頭檔案裡宣告。它不檢查溢位。
以下是 C 語言中 memcpy() 的語法:
void *memcpy(void *dest_str, const void *src_str, size_t number)
這裡,
dest_str − 指向目標陣列。
src_str − 指向源陣列。
number − 要從源複製到目標的位元組數。
以下是 C 語言中 memcpy() 的示例:
示例
#include <stdio.h> #include <string.h> int main () { char a[] = "Firststring"; const char b[] = "Secondstring"; memcpy(a, b, 5); printf("New arrays : %s\t%s", a, b); return 0; }
輸出
New arrays : SeconstringSecondstring
在上面的程式中,初始化了兩個 char 型別的陣列,並且 memcpy() 函式正在將源字串“b”複製到目標字串“a”。
char a[] = "Firststring"; const char b[] = "Secondstring"; memcpy(a, b, 5);
廣告