C/C++ 中的 memcpy()
在本文中,我們將討論 C++ STL 中 memcpy() 函式的工作原理、語法和示例。
什麼是 memcpy()?
memcpy() 函式是 C++ STL 中的一個內建函式,它在 <cstring> 標頭檔案中定義。memcpy() 函式用於複製記憶體塊。此函式用於將指定數量的值從一個儲存位置複製到另一個儲存位置。
該函式的結果是資料的二進位制副本。此函式不會檢查任何終止源或任何終止空字元,它只會從源複製 number 個位元組。
示例
void memcpy( void* destination, void* source, size_t num);
引數
此函式接受以下引數:
- dst - 這是指向我們要將輸出儲存到的位置的指標。
- src - 用作輸入的字串。
- n - 要複製的位元組數。
返回值
此函式返回將資料複製到的 dst 的指標。
示例
輸入
char str_1[10] = "Tutorials"; char str_2[10] = "Point"; memcpy (str_1, str_2, sizeof(str_2));
輸出
string str_1 before using memcpy Tutorials string str_1 after using memcpy Point
示例
#include <stdio.h> #include <string.h> int main (){ char str_1[10] = "Tutorials"; char str_2[10] = "Point"; puts("string str_1 before using memcpy "); puts(str_1); memcpy (str_1, str_2, sizeof(str_2)); puts("\nstring str_1 after using memcpy "); puts(str_1); return 0; }
輸出
string str_1 before using memcpy Tutorials string str_1 after using memcpy Point
示例
#include <stdio.h> #include <string.h> int main (){ char str_1[10] = "Tutorials"; char str_2[10] = "Point"; puts("string str_1 before using memcpy "); puts(str_1); memcpy (str_1,str_2, sizeof(str_2)); puts("\nstring str_2 after using memcpy "); puts(str_2); return 0; }
輸出
string str_1 before using memcpy Tutorials string str_2 after using memcpy Point
廣告