用C++實(shí)現(xiàn)strcpy(),返回一個(gè)char*類型的深入分析
更新時(shí)間:2013年05月29日 15:54:24 作者:
本篇文章是對(duì)用C++實(shí)現(xiàn)strcpy(),返回一個(gè)char*類型進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
代碼如下所示:
#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;
char* strcpy(char *src_str, char *dest_str)
{
char* dest = dest_str;
if ((src_str == NULL)||(dest_str == NULL)) //檢查指針有效性
{
throw "Invalid argument(s)"; //拋出異常
}
while((*dest_str++ = *src_str++) != '\0') //實(shí)現(xiàn)復(fù)制,包括末尾的‘/0'也復(fù)制了
{
NULL;
}
return dest;
}
int _tmain(int argc, _TCHAR* argv[])
{
char src[] = "Hello,world!";
char des[13] = {0};
strcpy(src, des);
cout << des << endl;
return 0;
}
復(fù)制代碼 代碼如下:
#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;
char* strcpy(char *src_str, char *dest_str)
{
char* dest = dest_str;
if ((src_str == NULL)||(dest_str == NULL)) //檢查指針有效性
{
throw "Invalid argument(s)"; //拋出異常
}
while((*dest_str++ = *src_str++) != '\0') //實(shí)現(xiàn)復(fù)制,包括末尾的‘/0'也復(fù)制了
{
NULL;
}
return dest;
}
復(fù)制代碼 代碼如下:
int _tmain(int argc, _TCHAR* argv[])
{
char src[] = "Hello,world!";
char des[13] = {0};
strcpy(src, des);
cout << des << endl;
return 0;
}
相關(guān)文章
C語(yǔ)言深入細(xì)致講解動(dòng)態(tài)內(nèi)存管理
動(dòng)態(tài)內(nèi)存是相對(duì)靜態(tài)內(nèi)存而言的。所謂動(dòng)態(tài)和靜態(tài)就是指內(nèi)存的分配方式。動(dòng)態(tài)內(nèi)存是指在堆上分配的內(nèi)存,而靜態(tài)內(nèi)存是指在棧上分配的內(nèi)存,本文帶你深入探究C語(yǔ)言中動(dòng)態(tài)內(nèi)存的管理2022-05-05詳解C++的String類的字符串分割實(shí)現(xiàn)
這篇文章主要介紹了詳解C++的String類的字符串分割實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2017-07-07淺談C++高并發(fā)場(chǎng)景下讀多寫(xiě)少的優(yōu)化方案
本文主要介紹了淺談C++高并發(fā)場(chǎng)景下讀多寫(xiě)少的優(yōu)化方案,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01static全局變量與普通的全局變量的區(qū)別詳細(xì)解析
以下是對(duì)static全局變量與普通的全局變量的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2013-09-09C語(yǔ)言設(shè)計(jì)前中后隊(duì)列實(shí)例代碼
隊(duì)列最主要的作用就是用來(lái)管理數(shù)據(jù)流的,防止數(shù)據(jù)因?yàn)閭鬏旑l率過(guò)快得不到及時(shí)處理而丟失,下面這篇文章主要給大家介紹了關(guān)于C語(yǔ)言設(shè)計(jì)前中后隊(duì)列的相關(guān)資料,需要的朋友可以參考下2021-12-12