欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C語言

當(dāng)前位置:主頁 > 軟件編程 > C語言 >

C++ 中strcpy標(biāo)準(zhǔn)寫法實(shí)例詳解

來源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C語言|點(diǎn)擊: 次

strcpy標(biāo)準(zhǔn)寫法

實(shí)例代碼:

// CppReference.cpp : 定義控制臺(tái)應(yīng)用程序的入口點(diǎn)。
//

#include "stdafx.h"
using namespace std;

/*
 * 說明:字符串拷貝版本1
 * 參數(shù):dest目標(biāo)地址,src源地址
 * 返回:返回拷貝好的地址;如果出錯(cuò)或者有重疊,無定義
 * 異常:可能出現(xiàn)字符串溢出,及dest所占空間不如src所占空間大。
 */
char *strcpy_v1(char *dest , const char *src)
{
  //調(diào)試時(shí),使用斷言,入口檢測
  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源地址
 * 返回:返回拷貝好的地址;如果出錯(cuò),無定義
 * 異常:可能出現(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(你能找出錯(cuò)誤的原因嗎)
 * 參數(shù):dest目標(biāo)地址,src源地址
 * 返回:返回拷貝好的地址;如果出錯(cuò),無定義
 * 異常:可能出現(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源地址
 * 返回:返回拷貝好的地址;如果出錯(cuò),無定義
 * 異常:可能出現(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源地址
 * 返回:返回拷貝好的地址;如果出錯(cuò),無定義
 * 異常:可能出現(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源地址
 * 返回:返回拷貝好的地址;如果出錯(cuò),無定義
 * 異常:可能出現(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源地址
 * 返回:返回拷貝好的地址;如果出錯(cuò),無定義
 * 異常:可能出現(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;
}

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有