strcpy的实现

#include <iostream>
#include <assert.h>
using namespace std;

/*
 字符串拷贝
 str_copy(目标字符串,源字符串)
 */
char *str_copy(char *des, const char* src){ //src所指向s的字符串不可修改
    assert((des!=NULL) && (src!=NULL));
    char *temp = des;
    while (*src != '\0') {
        *des++ = *src++; //等价于 *des=*src;des++;src++;
    }//等价于 while( (*des++ = *src++)!='\0')
    return temp;
}
int main(int argc, const char * argv[]) {
    const char *name = "scott";
    char str[20];
    cout<<str_copy(str, name)<<endl;//scott
    return 0;
}

results matching ""

    No results matching ""