網(wǎng)站首頁 編程語言 正文
定義
1.memcpy函數(shù)
void *memcpy(void *destin, void *source, unsigned n);
作用:函數(shù)memcpy從source指向的對象中復(fù)制n個字符到destin指向的對象中
返回值:函數(shù)memcpy返回destin的指針。
2.strcpy函數(shù)
char strcpy(char dest, const char *src);
作用:函數(shù)strcpy把src指向的串(包括空字符)復(fù)制到dest指向的數(shù)組中,src和dest所指內(nèi)存區(qū)域不可以重疊且dest必須有足夠的空間來容納src的字符串。
返回值:函數(shù)strcpy返回dest的指針。
3.strncpy函數(shù)
char *strncpy(char *destinin, char *source, int maxlen);
作用:復(fù)制字符串source中的內(nèi)容(字符,數(shù)字、漢字….)到字符串destinin中,復(fù)制多少由maxlen的值決定。source和destinin所指內(nèi)存區(qū)域不可以重疊且destinin必須有足夠的空間來容納source的字符長度+‘\0’。
返回值:函數(shù)strncpy返回destinin的值。
實(shí)現(xiàn)
看看strcpy函數(shù)的實(shí)現(xiàn)
char *myStrcpy(char *des,char *src){
if(des == NULL || src == NULL){
return NULL;
}//先看des和src的數(shù)據(jù)是否為null
char *bak = des;//取des地址賦bak指針
while(*src != 0){//當(dāng)src的數(shù)據(jù)(p的原始數(shù)據(jù))不為0,繼續(xù)執(zhí)行
*des = *src;//src的數(shù)據(jù)賦值給des
des++;
src++;//des和src的地址++下一個
}
*des = '\0';//while停止,也就是到src的數(shù)據(jù)位最后一位,此時令'\0'賦des
return bak;
}
strncpy函數(shù)的實(shí)現(xiàn)
char *myStrncpy(char *des,char *src,int count){
if(des == NULL || src == NULL){
return NULL;
}
char *bak = des;
while(*src != 0 && count > 0){
*des++ = *src++;//src的數(shù)據(jù)先賦值給des;src++;des++
count--;
}
if(count > 0){
while(count > 0){
*des++ = '\0';
count--;
}
return des;
}
*des = '\0';
return bak;
}
memcpy函數(shù)的實(shí)現(xiàn)
void * myMemcpy(void *dest, void *src, unsigned count)
{
if (dest == NULL || src == NULL)
{
return NULL;
}
char* pdest = (char*)dest;
char* psrc = (char*)src;
while (count--)
{
*pdest++ = *psrc++;
}
return dest;
}
區(qū)別
1、strcpy 是依據(jù) “\0” 作為結(jié)束判斷的,如果 dest 的空間不夠,則會引起 buffer overflow。
2、memcpy用來在內(nèi)存中復(fù)制數(shù)據(jù),由于字符串是以"\0"結(jié)尾的,所以對于在數(shù)據(jù)中包含"\0"的數(shù)據(jù)只能用memcpy。(通常非字符串的數(shù)據(jù)比如結(jié)構(gòu)體都會用memcpy來實(shí)現(xiàn)數(shù)據(jù)拷貝)
3、strncpy和memcpy很相似,只不過它在一個終止的空字符處停止。當(dāng)n>strlen(src)時,給dest不夠數(shù)的空間里填充"\0“;當(dāng)n<=strlen(src)時,dest是沒有結(jié)束符"\0“的。這里隱藏了一個事實(shí),就是dest指向的內(nèi)存一定會被寫n個字符。
4、strcpy只是復(fù)制字符串,但不限制復(fù)制的數(shù)量,很容易造成緩沖溢出。strncpy要安全一些。strncpy能夠選擇一段字符輸出,strcpy則不能。
總結(jié)
1、dest指向的空間要足夠拷貝;使用strcpy時,dest指向的空間要大于等于src指向的空間;使用strncpy或memcpy時,dest指向的空間要大于或等于n。
2、使用strncpy或memcpy時,n應(yīng)該大于strlen(s1),或者說最好n >= strlen(s1)+1;這個1 就是最后的“\0”。
3、使用strncpy時,確保s2的最后一個字符是"\0”。
原文鏈接:https://blog.csdn.net/qq_44333320/article/details/125715798
相關(guān)推薦
- 2022-04-16 Python實(shí)現(xiàn)杰卡德距離以及環(huán)比算法講解_python
- 2022-08-23 Python?Pandas數(shù)據(jù)處理高頻操作詳解_python
- 2022-07-19 C語言實(shí)現(xiàn)KMP算法+優(yōu)化
- 2022-09-02 六個Python編程最受用的內(nèi)置函數(shù)使用詳解_python
- 2022-06-21 分享四個python接口常用封裝函數(shù)_python
- 2022-06-19 微信小程序前端如何調(diào)用python后端的模型詳解_python
- 2022-11-03 Python?頁面解析Beautiful?Soup庫的使用方法_python
- 2023-01-17 Android資源文件與層次式導(dǎo)航超詳細(xì)講解_Android
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支