内存函数:memcpy、memmove、memcmp、memset(超详细讲解,小白一看就懂!!!!)
目錄
一、前言
二、內(nèi)存操作函數(shù)?
🍑memcpy
🍉 memmove
🍓memset函數(shù)
?🍌memcmp?
三、共勉
一、前言
? ? 在大家初學(xué)C語言的時(shí)候肯定會碰到對內(nèi)存的了解,但是估計(jì)大家會和我一樣,覺得這一部分不是很重要,所以草草就跳過去了,其實(shí)這里的內(nèi)存函數(shù)是最重要的,特別是在大家以后找工作的時(shí)候這里也是面試官最喜歡問的知識點(diǎn)。為了搞清楚這里的東西,專門花了一早上的時(shí)間去總結(jié),希望對大家有用!!!!
二、內(nèi)存操作函數(shù)?
🍑memcpy
知識點(diǎn)1:
memcpy()函數(shù)的頭文件 :#include <string.h>
memcpy()函數(shù)的聲明:
?其中 void * destination? 表示目標(biāo)函數(shù)? const void *source 表示原函數(shù)
?size_num 表示字節(jié)數(shù)
memcpy()函數(shù)的功能:可以對任何類型的數(shù)組進(jìn)行任意字節(jié)的拷貝
代碼演示:
#include<stdio.h> #include<string.h> int main() {int arr1[10] = { 1,2,3,4,5,6,7,8,9,10 };int arr2[20] = { 0 };memcpy(arr2, arr1, 20);//注意第3個(gè)參數(shù)的單位是字節(jié)// 結(jié)果 arr2 : 1 2 3 4 5return 0; }圖解:
?我們發(fā)現(xiàn)正如我們所料,向arr2數(shù)組傳送了 20個(gè)字節(jié),5個(gè)數(shù)據(jù)。
知識點(diǎn)2:
大家是否會考慮,對于strcpy()函數(shù)再拷貝字符串時(shí),如果遇到 '\0' 他會停止拷貝,那么思考memcpy()函數(shù)在拷貝字符串時(shí)遇到 '\0' 是否會停止。
代碼演示:
#include<stdio.h> #include<string.h> int main() {char arr1[] = "abc\0def";char arr2[20] = { 0 };memcpy(arr2, arr1, 6);printf("%s\n", arr2);return 0; }進(jìn)行圖解演示:
?結(jié)果發(fā)現(xiàn):memcpy在拷貝數(shù)據(jù)時(shí)與strcpy和strncpy不同的是memcpy遇到 '\0' 是不會停止拷貝到。?
知識點(diǎn) 3:
注意:?C語言標(biāo)準(zhǔn)規(guī)定memcpy()函數(shù)只拷貝不重疊的數(shù)據(jù)。
知識點(diǎn)4:
?memcpy()函數(shù)的模擬:
🍉 memmove
知識點(diǎn)1:
memmove()函數(shù)的頭文件: #include <string.h>
memmove()函數(shù)的聲明:
其中 void * destination? 表示目標(biāo)函數(shù)? const void *source 表示原函數(shù)
?size_num 表示字節(jié)數(shù)
memmove()函數(shù)的功能:可以對任何類型的數(shù)組進(jìn)行任意字節(jié)的拷貝。
注意:此時(shí)的memmmove 可以進(jìn)行重疊拷貝
代碼演示:
#include<stdio.h> #include<string.h> int main() {int arr1[10] = { 1,2,3,4,5,6,7,8,9,10 };memcpy(arr1 + 2, arr1, 20); // 拷貝5個(gè)字符過去,從數(shù)組的下標(biāo)3開始return 0; }圖解:
從上圖可以看見,memmove 實(shí)現(xiàn)了自己拷貝自己
知識點(diǎn)2:
memmove()函數(shù)的模擬實(shí)現(xiàn)
#include <stdio.h> #include <string.h> #include <ctype.h> #include <assert.h> void* my_memmove(void* dst, const void* src, size_t count) {void* ret = dst;assert(dst); //不為空指針assert(src);/** copy from lower addresses to higher addresses*/if (dst < src) // 區(qū)域1 從前向后拷貝{while (count--) // 傳出count次字節(jié){*(char*)dst = *(char*)src; // 因?yàn)槊看沃粋鬟f一個(gè)字節(jié) 需要將指針類型變?yōu)?#xff08;char *)dst = (char*)dst + 1;src = (char*)src + 1;}} else //區(qū)域二 從后向前拷貝{while (count--){*((char*)dst + count) = *((char*)src + count);}}return(ret); } int main() {int a[] = { 1,2,3,4,5,6,7,8,9,10 };int a1[10] = { 0 };my_memmove(a1, a, 20);for (int i = 0; i < 10; i++){printf("%d ", a1[i]);}return 0; }🍓memset函數(shù)
知識點(diǎn)1:
memset()函數(shù)的頭文件:#include <string.h>
memset()函數(shù)的聲明:
memset()函數(shù)的作用:
?將數(shù)組的元素,統(tǒng)一設(shè)置成一個(gè)元素。
代碼演示:
?
大家估計(jì)此時(shí)會想到結(jié)果因該給前5個(gè)數(shù)據(jù)賦值了 1 。我們看看圖解
我們發(fā)現(xiàn)此時(shí)出現(xiàn)了很大的數(shù)據(jù),發(fā)現(xiàn)了錯誤,我們正確的應(yīng)該怎么寫呢?
?
#include<stdio.h> #include<string.h> int main() {int arr[10] = { 0 };int i = 0;for (i = 0; i < 10; i++){memset(&arr[i], 1, 1);}return 0; }圖解:
?此時(shí)正確了。
?🍌memcmp?
知識點(diǎn)1:
memcmp()函數(shù)的頭文件:#include <string.h>
memcmp()函數(shù)的聲明:
?memcmp函數(shù)的應(yīng)用:這個(gè)函數(shù)非常類似于 strcmp()函數(shù),只不過是按字節(jié)去比較
返回值:
代碼演示:
#include <stdio.h> #include <string.h> #include <ctype.h> #include <assert.h> int main() {int a[] = { 1,2,3,4,5 }; // 01 00 00 00 02 00 00 00 03 00 00 00 04 00 00 00 05 00 00 00int b[] = { 1,2,3,0,0 }; // 01 00 00 00 02 00 00 00 03 00 00 00 00 00 00 00 00 00 00 00int ret=memcmp(a, b, 12); // 比較前3個(gè)數(shù)int rat = memcmp(a, b, 16); // 比較前4個(gè)數(shù)printf("%d\n", ret); // 0 相同就為0printf("%d\n", rat); // 1 數(shù)組 a 大于 數(shù)組 b 就輸出 1 否則就輸出 -1 return 0; }三、共勉
以下就是我對C語言內(nèi)存函數(shù)的理解,如果有不懂和發(fā)現(xiàn)問題的小伙伴,請?jiān)谠u論區(qū)說出來哦,同時(shí)我還會繼續(xù)更新對qsort()函數(shù)的理解,請持續(xù)關(guān)注我哦!!!!!?
?
總結(jié)
以上是生活随笔為你收集整理的内存函数:memcpy、memmove、memcmp、memset(超详细讲解,小白一看就懂!!!!)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C/C++ memset使用
- 下一篇: z370-A装机记录