字符串的指针例程得不到预期结果!!!大家帮我看看吧,谢谢!!!
#include<stdio.h>void copy_string(char from[],char to[])
{
int i=0;
while(from[i]!='\0')
{
to[i]=from[i];
i++;
}
to[i]='\0';
}
int main()
{
char *a="I am a teacher.";
char *b="you are a student.";
printf("string a=%s\nstring b=%s\n",a,b);
copy_string(a,b);
printf("string a=%s\nstring b=%s\n",a,b);
}
以上程序在gcc下编译可以通过的,但输出的结果却和预期的相差很远!!!
预期输出为:
string a=I am a teacher.
string b=you are a student.
string a=I am a teacher.
string b=I am a teacher.
实际输出为:
string a=I am a teacher.
string b=you are a student.