C語(yǔ)言內(nèi)存函數(shù) memcpy,memmove ,memcmp
一、memcpy函數(shù)
1.用法
memcpy表示內(nèi)存函數(shù),用法跟strcpy差不多, 都是作為拷貝函數(shù)存在
strcpy只能用于字符串函數(shù),而memcpy函數(shù)可以使用任意類型
在使用任意類型時(shí),肯定用到的參數(shù)是void
void* memcpy(const void *dest,const void *src, size_t sum);
這里的sum作為字節(jié)數(shù)傳遞
#include<stdio.h> #include<string.h> int main() { int arr[10]={0}; int arr2[10]={1,2,3,4,5,6,7,8,9,10}; memcpy(arr1,arr2,20);//1 2 3 4 5 /20作為字節(jié)數(shù) 即傳遞4個(gè)整形 return 0; }
2.模擬實(shí)現(xiàn)memcpy函數(shù)
#include<stdio.h> #incldue<assert.h> void*mymemcpy(const void*dest,const void*src,size_t sum)//size_t說(shuō)明作為無(wú)符號(hào)數(shù)存在 { assert(dest&&src); void*ret=dest; while(sum--)//void*的指針 無(wú)法解引用和自加自減,解引用不知道訪問(wèn)幾個(gè)字節(jié),自加自減不知道跳過(guò)結(jié)果字節(jié) { *(char*)dest=*(char*)src;//這里不使用自加的原因: 因?yàn)閺?qiáng)制類型轉(zhuǎn)換是一種臨時(shí)的狀態(tài) 如果到++時(shí),指針會(huì)變回void* dest=(char*)dest+1; src=(char*)src+1; } return ret; } int main() { int arr1[10]={0}; int arr2[10]={1,2,3,4,5,6,7,8,9,10}; mymemcpy(arr1,arr2,20);//傳遞4個(gè)整形 return 0; }
二、memmove函數(shù)
1.用法
memmove函數(shù)是用于處理內(nèi)存重疊的情況
參數(shù)同memcpy函數(shù)相同
voidmemmove(const void dest,const void*src,size_t sum);
#include<stdio.h> #include<string.h> int main() { int arr[]={1,2,3,4,5,6,7,8,9,10};//將 1 2 3 4 5傳給 3 4 5 6 7 memmove(arr+2,arr,20);//結(jié)果為 1 2 1 2 3 4 5 return 0; }
2.模擬實(shí)現(xiàn)memmove函數(shù)
#include<stdio.h> #include<assert.h> void*mymemmove(const void*dest,const void*src,size_t sum) { assert(dest&&src); void*ret=dest;//返回目的地的初始地址 if(src>dest)//src大于dest指針時(shí) 從前往后傳遞 { while(sum--) { *(char*)dest=*(char*)src; dest=(char*)dest+1; src=(char*)src+1; } } else//src小于dest指針時(shí) 從后往前傳遞 { while(sum--) { *(char*)(dest+sum)=*(char*)(src+sum); } } return ret; } int main() { int arr[]={1,2,3,4,5,6,7,8,9,10}; mymemmove(arr+2,arr,20);//20代表字節(jié)數(shù) 即傳遞5個(gè)整形 return 0;//1 2 1 2 3 4 5 }
當(dāng)3 4 5 6 7 傳遞給 1 2 3 4 5時(shí)
src>dest指針 從前往后傳遞
當(dāng)1 2 3 4 5傳遞給 3 4 5 6 7時(shí)
src>dest指針 從后往前傳遞
三、memcmp函數(shù)
1.用法
同strcmp函數(shù)用法差不多 都為比較函數(shù)
但strcmp只能比較字符串之間的大小
而 memcmp函數(shù)可以比較任意類型的大小
int memcmp(const void * s1,constvoid* s2,size_t sum);
#include<stdio.h> #include<string.h> int main() { int arr1[]={1,2,3,4,5}; int arr2[]={1,1,3,4,5}; memcmp(arr1,arr2,8);//8為字節(jié)數(shù) 即2個(gè)整形 return 0; }
2.模擬實(shí)現(xiàn)memcmp函數(shù)
#include<stdio.h> #include<assert.h> int memcmp(const void*s1,const void*s2,size_t sum) { assert(s1&&s2); char*pa=(char*)s1; char*pb=(char*)s2; while(sum--) { if(*pa==*pb)//兩者有相同才向后移 沒有就等sum減為0 輸出 看是大于還是小于 { pa++;//如果兩者一直相同 不影響判斷條件sum 出循環(huán)輸出 pb++; } } return *pa-*pb; } int main() { int arr1[]={1,2,3,4,5}; int arr2[]={1,1,3,4,5}; int ret= memcmp(arr1,arr2,8);//8為字節(jié)數(shù) 即2個(gè)整形 printf("%d\n",ret);//1 return 0; }
到此這篇關(guān)于C語(yǔ)言內(nèi)存函數(shù) memcpy,memmove ,memcmp的文章就介紹到這了,更多相關(guān)C語(yǔ)言內(nèi)存函數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語(yǔ)言中實(shí)現(xiàn)自定義數(shù)據(jù)類型的輸入輸出的方法和技巧
在 C 語(yǔ)言中,除了基本的數(shù)據(jù)類型(如整型、浮點(diǎn)型、字符型等),我們還經(jīng)常需要自定義數(shù)據(jù)類型來(lái)滿足特定的編程需求,所以本文給大家介紹了C語(yǔ)言中實(shí)現(xiàn)自定義數(shù)據(jù)類型的輸入輸出的方法和技巧,需要的朋友可以參考下2024-07-07C語(yǔ)言實(shí)現(xiàn)車輛信息管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)車輛信息管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03C++實(shí)現(xiàn)LeetCode(82.移除有序鏈表中的重復(fù)項(xiàng)之二)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(82.移除有序鏈表中的重復(fù)項(xiàng)之二),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07C++ 中strcpy標(biāo)準(zhǔn)寫法實(shí)例詳解
這篇文章主要介紹了C++ 中strcpy標(biāo)準(zhǔn)寫法實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-06-06DSP中浮點(diǎn)轉(zhuǎn)定點(diǎn)運(yùn)算--浮點(diǎn)與定點(diǎn)概述
本文主要介紹DSP中浮點(diǎn)與定點(diǎn)概述,很值得學(xué)習(xí)一下,需要的朋友可以參考一下。2016-06-06