马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
题目是将a数组copy到数组b。
我用了两种方法(子函数中被注释的是另一种方法)
但是运行结果都是这样。
不知道为什么,请大家帮忙看看。谢谢!#include <stdio.h>
void copy(char from[],char to[]);
int main()
{
char a[]="I like English very much!";
char b[]="I like C very much too!";
printf("The a string is: "); //输出原来的a字符串;
printf("%s\n",a);
printf("The b string is: "); //输出原来的b字符串;
printf("%s\n",b);
printf("\n\n");
copy(a,b);
printf("The a string is: %s\n",a);
printf("The b string is: %s\n",b); //输出更改后的a,b字符串;
return 0;
}
void copy(char from[],char to[])
{
int i=0;
/*
char *p1,*p2;
p1 = from;
p2 = to;
for(;*p1!='\0';p1++,p2++)
{
*p2=*p1;
}
*p2='\0';
*/
while(from[i]!='\0')
{
to[i]=from[i];
i++;
}
to[i]='\0';
}
两个数组长度不一样。肯定出错。 char a[]="I like English very much!";
char b[]="I like C very much too!!!";
|