長沙的企業(yè)網(wǎng)站建設(shè)陜西網(wǎng)站設(shè)計
零.導言
? ? ? ?當我們學習了strcpy和strncpy函數(shù)后,也許會疑惑整形數(shù)組要如何拷貝,而今天我將講解的memcpy函數(shù)便可以拷貝整形數(shù)組。
一.memcpy函數(shù)的使用
? ? ? ??memcpy函數(shù)是一種C語言內(nèi)存函數(shù),可以按字節(jié)拷貝任意類型的數(shù)組,比如整形數(shù)組。
? ? ? ? 我們直接上代碼:
int arr1[10] = { 1,2,3,4,5,6,7 };
int arr2[10] = { 0 };
memcpy(arr2, arr1, 7 * 4);
????????這樣,arr1的內(nèi)容就拷貝到arr2里去了。
? ? ? ? 驗證結(jié)果:
二.memcpy函數(shù)實現(xiàn)數(shù)組部分拷貝
????????其實很簡單,如下:
int arr1[10] = { 1,2,3,4,5,6,7 };
int arr2[10] = { 0 };
memcpy(arr2, arr1 + 3, 7 * 4);
? ? ? ??這樣,對arr1的拷貝就會跳過arr1的前三個元素,驗證:
三.memcpy函數(shù)使用的意義
?????????簡化了代碼,把原來需要循環(huán)實現(xiàn)的代碼簡化成了函數(shù),使代碼的實現(xiàn)更加方便。
四.相關(guān)鏈接
????????【C/C++】字符/字符串函數(shù)(1)——由string.h提供-CSDN博客
????????【C/C++】memcpy函數(shù)的模擬實現(xiàn)-CSDN博客
完