#include<stdio.h>
void strcpy(char *to,const char * from)
{
while(*from)
{
from++;
}
from--;
while (*from)
{
to[0]=*from;
to++;
from--;
}
to[0]='\0';
}int main(void)
{
char *from="zhongguo",*to;
strcpy(to,from);
printf("to=%s\n",to);
getch();
return;
}
void strcpy(char *to,const char * from)
{
while(*from)
{
from++;
}
from--;
while (*from)
{
to[0]=*from;
to++;
from--;
}
to[0]='\0';
}int main(void)
{
char *from="zhongguo",*to;
strcpy(to,from);
printf("to=%s\n",to);
getch();
return;
}
1. main应该返回一个整数,但是你没有。
2. 自定义函数strcpy与系统函数重名。
3. 目标字符串to是悬浮指针,空间都没有,也能用来存放字符串?在修改上述3个错误后,编译通过了,但是一运行,程序崩溃了,估计是因为to没有分配空间引起的。
from 是声明的特殊性,即from是分配在全局静态存储区,默认都初始化为零的,所以从后
向前减能结束,但是换成在栈中分配的话,程序运行不正确了
char from[] ="zhongguo",*to;//栈中分配上述代码还有问题 主要是 *to应该定义为 char to[];