1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
//
//C语言标准库函数strcpy的一种典型的工业级的最简实现。
//返回值:目标串的地址。
//对于出现异常的情况ANSI-C99标准并未定义,故由实现者决定返回值,通常为NULL。
//参数:des为目标字符串,source为原字符串。
char
*
strcpy
(
char
* des,
const
char
* source)
{
char
* r=des;
assert
((des != NULL) && (source != NULL));
while
((*des++ = *source++)!=
'\0'
);
return
r;
}
//while((*des++=*source++));的解释:赋值表达式返回左操作数,所以在赋值NULL后,循环停止。
|
- #include<stdio.h>
- #include<string.h>
- void main()
- {
- char s[]="123456789";
- char d[]="123";
- strcpy(d,s);
- printf("d=%s,\ns=%s",d,s);
- }
- #include<stdio.h>
- #include<string.h>
- void main()
- {
- char d[]="123";
- char s[]="123456789";
- strcpy(d,s);
- printf("d=%s,\ns=%s",d,s);
- }
- //已知strcpy函数的原型是:
- char * strcpy(char * strDest,const char * strSrc);
- //实现代码
- char * strcpy(char * strDest,const char * strSrc)
- {
- if ((strDest==NULL)||(strSrc==NULL))
- throw "Invalid argument(s)";
- char * strDestCopy=strDest;
- while ((*strDest++=*strSrc++)!='\0');
- return strDestCopy;
- }