c语言,两个字符数组,利用一个函数将一个内容复制并替换另一个的内容 5
运行正确:#include<stdio.h>intmain(){voidcopy_string(char*from,char*to);char*a="Iamateache...
运行正确:
#include <stdio.h>
int main()
{void copy_string(char *from, char *to);
char *a="I am a teacher.";
char b[]="you are a student.";
char *t=b;
printf("string a=%s\nstring b=%s\n",a,b);
printf("copy string a to string b:\n");
copy_string(a,t);
printf("\nstring a=%s\nstring b=%s\n",a,b);
return 0;
}
void copy_string(char *from, char *to)
{ for(;*from!='\0';from++,to++)
*to=*from;
*to='\0';
}
然而将第一个字符数组用数组保存,第二个用指针指向后就不能正确运行
不能运行的:
#include <stdio.h>
int main()
{void copy_string(char *from, char *to);
char *a="I am a teacher."//有改动
char b[]="you are a student."//有改动
char *t=b;//有改动
printf("string a=%s\nstring b=%s\n",a,b);
printf("copy string a to string b:\n");
copy_string(a,t);
printf("\nstring a=%s\nstring b=%s\n",a,b);
return 0;
}
void copy_string(char *from, char *to)
{ for(;*from!='\0';from++,to++)
*to=*from;
*to='\0';
}
改动后的程序发错了
改动后为以下:
#include <stdio.h>
int main()
{void copy_string(char *from, char *to);
char a[]="I am a teacher.";//有改动
char *b="you are a student.";//有改动
char *t=a;//有改动
printf("string a=%s\nstring b=%s\n",a,b);
printf("copy string a to string b:\n");
copy_string(t,b);
printf("\nstring a=%s\nstring b=%s\n",a,b);
return 0;
}
void copy_string(char *from, char *to)
{ for(;*from!='\0';from++,to++)
*to=*from;
*to='\0';
} 展开
#include <stdio.h>
int main()
{void copy_string(char *from, char *to);
char *a="I am a teacher.";
char b[]="you are a student.";
char *t=b;
printf("string a=%s\nstring b=%s\n",a,b);
printf("copy string a to string b:\n");
copy_string(a,t);
printf("\nstring a=%s\nstring b=%s\n",a,b);
return 0;
}
void copy_string(char *from, char *to)
{ for(;*from!='\0';from++,to++)
*to=*from;
*to='\0';
}
然而将第一个字符数组用数组保存,第二个用指针指向后就不能正确运行
不能运行的:
#include <stdio.h>
int main()
{void copy_string(char *from, char *to);
char *a="I am a teacher."//有改动
char b[]="you are a student."//有改动
char *t=b;//有改动
printf("string a=%s\nstring b=%s\n",a,b);
printf("copy string a to string b:\n");
copy_string(a,t);
printf("\nstring a=%s\nstring b=%s\n",a,b);
return 0;
}
void copy_string(char *from, char *to)
{ for(;*from!='\0';from++,to++)
*to=*from;
*to='\0';
}
改动后的程序发错了
改动后为以下:
#include <stdio.h>
int main()
{void copy_string(char *from, char *to);
char a[]="I am a teacher.";//有改动
char *b="you are a student.";//有改动
char *t=a;//有改动
printf("string a=%s\nstring b=%s\n",a,b);
printf("copy string a to string b:\n");
copy_string(t,b);
printf("\nstring a=%s\nstring b=%s\n",a,b);
return 0;
}
void copy_string(char *from, char *to)
{ for(;*from!='\0';from++,to++)
*to=*from;
*to='\0';
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询