#include <stdio.h>
#include <curses.h>
int main()
{
void copy_string(char from[], char to[]);
char a[] = "I am a teacher";
char b[] = "you my student";
char * from = a, * to = b;
printf("string a = %s\n string b = %s\n",a,b);
printf("\ncopy string a to string b:\n");
copy_string(from, to);
printf("string a = %s\nstring b = %s\n",a,b);
return 0;
}
void copy_string(char from[],char to[])
{
int i = 0;
while(from[i]!='\0')
{
to[i++]=from[i++];
}
to[i]='\0';
}
第一段输出结果为:
string a = I am a teacher
string b = you my student
copy string a to string b:
string a = I am a teacher
string b = om aytsauhert
#include <stdio.h>
#include <curses.h>
int main()
{
void copy_string(char from[], char to[]);
char a[] = "I am a teacher";
char b[] = "you my student";
char * from = a, * to = b;
printf("string a = %s\n string b = %s\n",a,b);
printf("\ncopy string a to string b:\n");
copy_string(from, to);
printf("string a = %s\nstring b = %s\n",a,b);
return 0;
}
void copy_string(char from[],char to[])
{
int i = 0;
while(from[i]!='\0')
{
to[i]=from[i];
i++;
}
to[i]='\0';
}
想请教的问题是:
to[i]=from[i];
i++;
改成了
to[i++]=from[i++];
之后为什么结果是不对的?
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.