字符串拷貝函數strcpy寫法_轉


Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->// CppReference.cpp : 定義控制台應用程序的入口點。
//

#include "stdafx.h"
using namespace std;

/*
 * 說明:字符串拷貝版本1
 * 參數:dest目標地址,src源地址
 * 返回:返回拷貝好的地址;如果出錯或者有重疊,無定義
 * 異常:可能出現字符串溢出,及dest所占空間不如src所占空間大。
 */
char *strcpy_v1(char *dest , const char *src)
{
    //調試時,使用斷言,入口檢測
    assert( (dest!=NULL) && (src!=NULL) );
    
    //注意這里的內存指向參數dest所在的內存,不是棧內存,因而可以在函數中返回
    char *to = dest;
    
    //主要操作在while條件中完成
    while( (*dest++ = *src++)!='\0')
    {
        NULL;    
    }
    
    //返回拷貝字符串首地址,方便連綴,比如strlen(strcpy(dest,"hello"))
    return to;
}

/*
 * 說明:字符串拷貝版本2
 * 參數:dest目標地址,src源地址
 * 返回:返回拷貝好的地址;如果出錯,無定義
 * 異常:可能出現字符串溢出,及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(你能找出錯誤的原因嗎)
 * 參數:dest目標地址,src源地址
 * 返回:返回拷貝好的地址;如果出錯,無定義
 * 異常:可能出現字符串溢出,及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
 * 參數:dest目標地址,src源地址
 * 返回:返回拷貝好的地址;如果出錯,無定義
 * 異常:可能出現字符串溢出,及dest所占空間不如src所占空間大。
 */
char *strcpy_v3(char *dest , const char *src)
{
    char *d = dest;
    char c;
    
    while(*src)
        *dest++ = *src++;
        
    *dest='\0';
    
    return d;
}

/*
 * 說明:字符串拷貝版本4
 * 參數:dest目標地址,src源地址
 * 返回:返回拷貝好的地址;如果出錯,無定義
 * 異常:可能出現字符串溢出,及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
 * 參數:dest目標地址,src源地址
 * 返回:返回拷貝好的地址;如果出錯,無定義
 * 異常:可能出現字符串溢出,及dest所占空間不如src所占空間大。restrict關鍵字限定字符串不能重疊。
 */
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
 * 參數:dest目標地址,src源地址
 * 返回:返回拷貝好的地址;如果出錯,無定義
 * 異常:可能出現字符串溢出,及dest所占空間不如src所占空間大。restrict關鍵字限定字符串不能重疊。
 */
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;
}

轉自:http://www.cnblogs.com/zxher/archive/2010/07/20/1781209.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM