C++ 中strcpy標(biāo)準(zhǔn)寫法實(shí)例詳解
strcpy標(biāo)準(zhǔn)寫法
實(shí)例代碼:
// CppReference.cpp : 定義控制臺應(yīng)用程序的入口點(diǎn)。
//
#include "stdafx.h"
using namespace std;
/*
* 說明:字符串拷貝版本1
* 參數(shù):dest目標(biāo)地址,src源地址
* 返回:返回拷貝好的地址;如果出錯或者有重疊,無定義
* 異常:可能出現(xiàn)字符串溢出,及dest所占空間不如src所占空間大。
*/
char *strcpy_v1(char *dest , const char *src)
{
//調(diào)試時,使用斷言,入口檢測
assert( (dest!=NULL) && (src!=NULL) );
//注意這里的內(nèi)存指向參數(shù)dest所在的內(nèi)存,不是棧內(nèi)存,因而可以在函數(shù)中返回
char *to = dest;
//主要操作在while條件中完成
while( (*dest++ = *src++)!='\0')
{
NULL;
}
//返回拷貝字符串首地址,方便連綴,比如strlen(strcpy(dest,"hello"))
return to;
}
/*
* 說明:字符串拷貝版本2
* 參數(shù):dest目標(biāo)地址,src源地址
* 返回:返回拷貝好的地址;如果出錯,無定義
* 異常:可能出現(xiàn)字符串溢出,及dest所占空間不如src所占空間大。
*/
char *strcpy_v2(char *dest , const char *src)
{
char *d = dest;
char c;
while((c=*src++) != '\0')
{
*(dest++)=c;
}
*dest='\0';
return d;
}
/*
* 說明:字符串拷貝版本2(你能找出錯誤的原因嗎)
* 參數(shù):dest目標(biāo)地址,src源地址
* 返回:返回拷貝好的地址;如果出錯,無定義
* 異常:可能出現(xiàn)字符串溢出,及dest所占空間不如src所占空間大。
*/
char *strcpy_v2_error(char *dest , const char *src)
{
char *d = dest;
char c;
while((c=*src++) != '\0')
{
*(d++)=c;
}
*d='\0';
return d;
}
/*
* 說明:字符串拷貝版本3
* 參數(shù):dest目標(biāo)地址,src源地址
* 返回:返回拷貝好的地址;如果出錯,無定義
* 異常:可能出現(xiàn)字符串溢出,及dest所占空間不如src所占空間大。
*/
char *strcpy_v3(char *dest , const char *src)
{
char *d = dest;
char c;
while(*src)
*dest++ = *src++;
*dest='\0';
return d;
}
/*
* 說明:字符串拷貝版本4
* 參數(shù):dest目標(biāo)地址,src源地址
* 返回:返回拷貝好的地址;如果出錯,無定義
* 異常:可能出現(xiàn)字符串溢出,及dest所占空間不如src所占空間大。
*/
char *strcpy_v4(char *dest , const char *src)
{
char *d = dest;
char c;
while( (*dest = *src)!='\0')
{
src++;
dest++;
}
*dest='\0';
return d;
}
/*
* 說明:字符串拷貝版本5
* 參數(shù):dest目標(biāo)地址,src源地址
* 返回:返回拷貝好的地址;如果出錯,無定義
* 異常:可能出現(xiàn)字符串溢出,及dest所占空間不如src所占空間大。restrict關(guān)鍵字限定字符串不能重疊。
*/
char *strcpy_v5(char* _restrict dest , const char* _restrict src)
{
char *d = dest;
char c;
while( (*dest = *src)!='\0')
{
src++;
dest++;
}
*dest='\0';
return d;
}
/*
* 說明:字符串拷貝版本6
* 參數(shù):dest目標(biāo)地址,src源地址
* 返回:返回拷貝好的地址;如果出錯,無定義
* 異常:可能出現(xiàn)字符串溢出,及dest所占空間不如src所占空間大。restrict關(guān)鍵字限定字符串不能重疊。
*/
char *strcpy_v6(char* _restrict dest , const char* _restrict src)
{
char *d = dest;
char c;
while(*dest++=*src++);
return d;
}
int _tmain(int argc, _TCHAR* argv[])
{
char buf[512];
char *buf2 = (char *)calloc(50,sizeof(char));
char *buf3 = (char *)malloc(50*sizeof(char));
char *buf5 = (char *)malloc(50*sizeof(char));
char *buf6 = (char *)malloc(50*sizeof(char));
printf("using strcpy_v1,the string 'Hello,World'\'s length is : %d\n",strlen(strcpy_v1(buf,"Hello,World")));
printf("using strcpy_v2,the string 'This is the best age'\'s length is : %d\n",strlen(strcpy_v2(buf2,"This is the best age")));
printf("using strcpy_v2,the string 'This is the best age'\'s length is : %d\n",strlen(strcpy_v2_error(buf2,"This is the best age")));
printf("using strcpy_v3,the string 'This is the best age'\'s length is : %d\n",strlen(strcpy_v3(buf3,"This is the best age")));
printf("using strcpy_v5,the string 'This is the best age'\'s length is : %d\n",strlen(strcpy_v5(buf5,"This is the best age")));
printf("using strcpy_v6,the string 'This is the best age'\'s length is : %d\n",strlen(strcpy_v6(buf6,"This is the best age")));
system("pause");
return 0;
}
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
上一篇:淺談C++類型轉(zhuǎn)化(運(yùn)算符重載函數(shù))和基本運(yùn)算符重載(自增自減)
欄 目:C語言
下一篇:C\C++ 獲取當(dāng)前路徑實(shí)例詳解
本文標(biāo)題:C++ 中strcpy標(biāo)準(zhǔn)寫法實(shí)例詳解
本文地址:http://m.jygsgssxh.com/a1/Cyuyan/1476.html
您可能感興趣的文章
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對數(shù)怎么表達(dá)
- 04-02c語言沒有round函數(shù) round c語言
- 04-02C語言中怎么打出三角函數(shù) c語言中怎么打出三角函數(shù)的值
- 01-10深入理解C++中常見的關(guān)鍵字含義
- 01-10使用C++實(shí)現(xiàn)全排列算法的方法詳解
- 01-10深入Main函數(shù)中的參數(shù)argc,argv的使用詳解
- 01-10APUE筆記之:進(jìn)程環(huán)境詳解
- 01-10c++中inline的用法分析
- 01-10如何尋找數(shù)組中的第二大數(shù)


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機(jī)閱讀
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10C#中split用法實(shí)例總結(jié)
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10delphi制作wav文件的方法
- 04-02jquery與jsp,用jquery
- 01-10使用C語言求解撲克牌的順子及n個骰子


