该程序运行异常,错在哪
该程序运行错误,错在哪?
#include<stdio.h>
void my_strcpy(char *to,char *from)
{
while((*to++=*from++)!='\0');
}
void main()
{
char *to="";
char *from="hello world!";
printf("to=%s,from=%s\n",to,from);
my_strcpy(to,from);
printf("to=%s,from=%s\n",to,from);
}
------解决方案--------------------
char *to="";
这些都是常量了
没法改。。。
------解决方案--------------------
#include <stdio.h>
void my_strcpy(char *to,char *from)
{
while((*to++=*from++)!='\0');
}
void main()
{
char *to=new char[100] ; char *from="hello world!";
printf("to=%s,from=%s\n",to,from);
my_strcpy(to,from);
printf("to=%s,from=%s\n",to,from);
}
------解决方案--------------------
动态分配
#include<stdio.h>
void my_strcpy(char *to,char *from)
{
while((*to++=*from++)!='\0');
}
void main()
{
char *to="";
char *from="hello world!";
printf("to=%s,from=%s\n",to,from);
my_strcpy(to,from);
printf("to=%s,from=%s\n",to,from);
}
------解决方案--------------------
char *to="";
这些都是常量了
没法改。。。
------解决方案--------------------
#include <stdio.h>
void my_strcpy(char *to,char *from)
{
while((*to++=*from++)!='\0');
}
void main()
{
char *to=new char[100] ; char *from="hello world!";
printf("to=%s,from=%s\n",to,from);
my_strcpy(to,from);
printf("to=%s,from=%s\n",to,from);
}
------解决方案--------------------
动态分配
- C/C++ code
char *to=new char[20];
------解决方案--------------------
你main中的to 是常量字符串,换成 char to[128]={0};或者
在第一个 printf或malloc下