在本教程中,我們將借助示例了解 C++ memcpy() 函數。
C++ 中的memcpy()
函數將指定字節的數據從源複製到目標。它在cstring 頭文件中定義。
示例
#include <cstring>
#include <iostream>
using namespace std;
int main() {
char source[] = "Tutorial";
char destination[] = "Programiz";
// copy all bytes of destination to source
memcpy(destination, source, sizeof(source));
cout << destination;
return 0;
}
// Output: Tutorial
memcpy() 語法
用法:
memcpy(void* dest, const void* src, size_t count);
參數:
memcpy()
函數接受以下參數:
- dest- 指向內容複製到的內存位置的指針。它是
void*
類型。 - src- 指向從中複製內容的內存位置的指針。它是
void*
類型。 - count- 要複製的字節數
src
到dest
.它是size_t
類型。
注意:自從src
和dest
屬於void*
類型,我們可以使用大多數數據類型memcpy()
.
返回:
memcpy() 函數返回:
dest
- 目標的內存位置
memcpy() 原型
cstring 頭文件中定義的memcpy()
原型為:
void* memcpy(void* dest, const void* src,size_t count);
當我們調用此函數時,它會將 count
字節從 src
指向的內存位置複製到 dest
指向的內存位置。
memcpy() 未定義行為
的行為memcpy()
是不明確的如果:
src
或dest
是空指針,或者- 對象重疊。
示例 1:C++ memcpy()
#include <cstring>
#include <iostream>
using namespace std;
int main() {
char source[] = "Tutorial";
char destination[] = "Programiz";
cout << "Initial destination: " << destination << endl;
// copy all bytes of destination to source
memcpy(destination, source, sizeof(source));
cout << "Final destination: " << destination;
return 0;
}
輸出
Initial destination: Programiz Final destination: Tutorial
在這裏,我們使用sizeof()
函數將source
的所有字節複製到destination
。
示例 2:C++ memcpy() - 僅複製部分源代碼
#include <cstring>
#include <iostream>
using namespace std;
int main() {
char source[] = "Tutorial";
char destination[] = "Programiz";
cout << "Initial destination: " << destination << endl;
// copy 4 bytes of destination to source
memcpy(destination, source, 4);
cout << "Final destination: " << destination;
return 0;
}
輸出
Initial destination: Programiz Final destination: Tutoramiz
在這裏,我們隻複製了 source
的 4 個字節到 destination
。
由於單個 char
數據占用 1 個字節,因此該程序將 destination
的前 4 個字符替換為 source
的前 4 個字符。
示例 3:具有整數類型的 C++ memcpy()
#include <cstring>
#include <iostream>
using namespace std;
int main() {
int source[10] = {8,3,11,61,-22,7,-6,2,13,47};
int destination[5];
// copy 5 elements (20 bytes) of source to destination
memcpy(destination, source, sizeof(int) * 5);
cout << "After copying" << endl;
for (int i=0; i<5; i++)
cout << destination[i] << endl;
return 0;
}
輸出
After copying 8 3 11 61 -22
在這裏,我們創建了兩個int
數組source[]
和destination[]
大小10和5分別。
然後我們使用 memcpy()
函數將 source[]
的 5 個元素複製到 destination[]
。
memcpy(destination, source, sizeof(int) * 5);
注意參數 sizeof(int) * 5
。代碼 sizeof(int)
給出了單個 int
數據占用的總字節數,即 4 個字節。
由於我們要將 5 個 int
元素從 source[]
複製到 destination[]
,因此我們將 sizeof(int)
乘以 5
,等於 20 個字節的數據。
相關用法
- C++ memcpy()用法及代碼示例
- C++ memchr()用法及代碼示例
- C++ memcmp()用法及代碼示例
- C++ memset()用法及代碼示例
- C++ memmove()用法及代碼示例
- C++ merge()用法及代碼示例
- C++ map lower_bound()用法及代碼示例
- C++ multimap key_comp()用法及代碼示例
- C++ multimap empty()用法及代碼示例
- C++ multimap cend()用法及代碼示例
- C++ multiset begin()、end()用法及代碼示例
- C++ mbtowc()用法及代碼示例
- C++ map::at()用法及代碼示例
- C++ multiset value_comp()用法及代碼示例
- C++ multimap insert()用法及代碼示例
- C++ map max_size()用法及代碼示例
- C++ multiset emplace()用法及代碼示例
- C++ multiset lower_bound()用法及代碼示例
- C++ multimap lower_bound()用法及代碼示例
- C++ multiset crbegin()、crend()用法及代碼示例
注:本文由純淨天空篩選整理自 C++ memcpy()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。