strcpy(), 字符串拷贝.
char *strcpy(char *destination, const char *source)
{
while(*destinaton++=*source++);
return (destination-1);
}为什么
return (destination-1);
而不
return (destination);

解决方案 »

  1.   

    这个函数的返回值时返回拷贝到的字符个数,不包括NULL结束符。
      

  2.   

    就是看的下面的:很多人认为C语言中的难点是指针,对指针的理解直接关系到所编程序的好坏,所以,
    在这里列举了一些C编译器通常都有的标准函数的源代码,看过它们,就能对指针和字符串
    有所了解了.
    1. strlen(),计算字符串长度
    int strlen(const char string)
    {
    int i=0;
    while(string[i]) i++;
    return i;
    }
    2. strcpy(), 字符串拷贝.
    char *strcpy(char *destination, const char *source)
    {
    while(*destinaton++=*source++);
    return (destination-1);
    }
    3. strcat(), 字符串的连接.
    char *strcat(char *target,const char *source)
    {
    char *original=target;
    while(*target) target++; // Find the end of the string
    while(*target++=*source++);
    return(original);
    }
    4. streql(), 判断两个字符串是否相等.
    int streql(char *str1,char *str2)
    {
    while((*str1==*str2)&&(*str1))
    {
    str1++;
    str2++;
    }
    return((*str1==NULL)&&(*str2==NULL));
    }
    5. strchr(), 在字符串中查找某个字符.
    char *strchr(const char *string,int letter)
    {
    while((*string!=letter)&(*string))
    string++;
    return (string);
    }
    6. chrcnt(), 计算某个字符在字符串中出现的次数.
    int chrcnt(const char *string,int letter)
    {
    int count=0;
    while(*string)
    if(*string==letter)count++;
    return count;
    }
    7. strcmp(), 判断两个字符串是否相等.
    int strcmp(const char *str1,const char *str2)
    {
    while((*str1==*str2)&&(*str1))
    {
    str1++;
    str2++;
    }
    if((*str1==*str2)&&(!*str1)) //Same strings
    return o;
    else if((*str1)&&(!*str2)) //Same but str1 longer
    return -1;
    else if((*str2)&&(!*str1)) //Same but str2 longer
    else
    return((*str1>*str2)?-1:1);
    }
      

  3.   

    你可以仔细分析一下:
        在执行WHILE函数时destinaton++多加了一次
        所以return (destination-1);
        而不
           return (destination);想一想do{...
            i++;
            }while(i<100)
         和while(i<100)
            {.....
             i++;
            }区别
      

  4.   

    while(*destinaton++=*source++);//这一句将source指针所在的字符赋给destination;如果这时字符串destination(就是source所指的那个字符,只有一个字符的字符串)不为Null,继续执行while语句。然后destination 和source指针执行++操作,向后移动一位。直到source指针指到字符串结尾,这时*destination=null,跳出while语句,进入下一句话。这时候,destination是指在想要得字符串的末尾的。return (destination-1);//l是字符串长度吧?这句把指针指向目的字符串的开始位置。如果return (destination);得到的将是空字符串。
      

  5.   

    我觉得是l(long的第一个字母:)),不是 1,hehe,不知道我有没有理解错误。*destination++ 应该是*(destination++)的意思吧。因为目的是strcpy,如果理解为(*destination)++是没有意思的。(*destinaton++=*source++)是一个一个字符赋值循环到*source为null时候结束。这时候*destination也是null
      

  6.   

    执行这段代码就知道了。
    ft!#include <iostream>char *strcp(char *dest, const char *source)
    {
    while(*dest++=*source++);
    return(dest-1);
    }
    int main()
    {
        char stra[]= "abc";
        char strtemp[3];
        char *ptemp;
        ptemp = strcp(strtemp,stra);
        std::cout << "stra: "  << stra << endl;
        std::cout << "strtemp: "  <<  strtemp << endl;
        std::cout << "ptemp: " << ptemp << endl;
        std::cout << "&strtemp: " << &strtemp << endl;
        std::cout << "&ptemp:   " << &ptemp << endl;
        return 0;
    }