拷贝字符串出现的问题
#include<stdio.h>#include<string.h>
main()
{
static char *s="Chinese";
static char *t="American";
printf("\n\ns=%s",s);
printf("\nt=%s",t);
strcpy(s,t);
printf("Copy After:\n");
printf("\ns=%s",s);
printf("\nt=%t",t);
}
结果为:
s=Chinese
t=American
Copy After:
s=American
t=
为什么拷贝完字符串后t的值会丢失?如果不定义成常量,用scanf函数读入两个相同的字符串,再进行复制拷贝,就不会出现字符串丢失现象。什么地方出错了?请高手指教。
#include<stdio.h>
#include<string.h>
main()
{
char *s,*t;
scanf("%s%s",s,t);
printf("\n\ns=%s",s);
printf("\nt=%s",t);
strcpy(s,t);
printf("Copy After:\n");
printf("\ns=%s",s);
printf("\nt=%t",t);
}
结果为:
s=Chinese
t=American
Copy After:
s=American
t=American