/*v1*/
void strcpy (char *s, char *t)
{
int i;
i = 0;
while ((s[i] = T[i])! = ' + ')
i++;
}
/*v2*/
void strcpy (char *s, char *t)
{
while ((*s = *t)! = ' + ') {
s++;
t++;
}
}
/*v3*/
void strcpy (char *s, char *t)
{
while ((*s++ = *t++)! = ' + ')
;
}
/*v4:final Verison in <<the C programming language>>2nd*/
void strcpy (char *s, char *t)
{
while (*s++ = *t++)
;
}
In fact, the above version also evolved into the actual interview encountered:
/*v4*/
void strcpy (char *s, const char *t)
{
while (*s++ = *t++)
;
}
/*v5*/
char* strcpy (char *s, const char *t)
{
char *scopy = s;
while (*s++ = *t++)
;
return sCopy;
}
/*v5*/
char* strcpy (char *s, const char *t)
{
if (S==null | | t==null)
return NULL;
char *scopy = s;
while (*s++ = *t++)
;
return sCopy;
}
Today is summarized as follows (hope to be added later):
V1: array subscript, although a local variable is used, but the code is easy to read.
V2: modified to a simple pointer version.
V3: That's what k&d is trying to emphasize in his book. Is that the use of + + will make the code concise.
V4: The GCC compiler will give a warning under the current Mac
Warning message: Warning: Using the result of an assignment as a condition without parentheses [-wparentheses]
While (*dest++ = *src++)
Compiler information:
Configured with:--prefix=/library/developer/commandlinetools/usr--with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 6.0 (clang-600.0.57) (based on LLVM 3.5SVN)
target:x86_64-apple-darwin14.1.0
Thread Model:posix
V5: The change of formal parameters will not be the same as the previous use of the C standard library of the program, but in terms of function function, this change is correct.
V6: Here is a question, the empty string check is not strcpy part of the matter.
The evolution and original intention of C language programming string copy copy