请给我找找错误运用指针编写程序:输入三个字符串,按由小到大的顺序输出.#include#includevoid main(){char swap(char *p1,char *p2);char n1[20],n2[20],n3[20];char *pointer1,*pointer2,*pointer3;scanf("%s",n1);scanf("%s",n2);s
来源:学生作业帮助网 编辑:六六作业网 时间:2024/12/24 02:36:31
请给我找找错误运用指针编写程序:输入三个字符串,按由小到大的顺序输出.#include#includevoid main(){char swap(char *p1,char *p2);char n1[20],n2[20],n3[20];char *pointer1,*pointer2,*pointer3;scanf("%s",n1);scanf("%s",n2);s
请给我找找错误
运用指针编写程序:输入三个字符串,按由小到大的顺序输出.
#include
#include
void main()
{
char swap(char *p1,char *p2);
char n1[20],n2[20],n3[20];
char *pointer1,*pointer2,*pointer3;
scanf("%s",n1);
scanf("%s",n2);
scanf("%s",n3);
pointer1=&n1;
pointer2=&n2;
pointer3=&n3;
if(strcmp(n1,n2)>0) swap(pointer1,pointer2);
if(strcmp(n1,n3)>0) swap(pointer1,pointer3);
if(strcmp(n2,n3)>0) swap(pointer2,pointer3);
printf("%s,%s,%s\n",n1,n2,n3);
}
char swap(char *p1,char *p2)
{
char p[20];
p[20]=*p1,*p1=*p2,*p2=p[20];
}
我这样写,哪里错了,
请给我找找错误运用指针编写程序:输入三个字符串,按由小到大的顺序输出.#include#includevoid main(){char swap(char *p1,char *p2);char n1[20],n2[20],n3[20];char *pointer1,*pointer2,*pointer3;scanf("%s",n1);scanf("%s",n2);s
pointer1=&n1;
pointer2=&n2;
pointer3=&n3;
这个地方右边的数组名就是首地址,不用再加地址符号了
另外,定义的函数中p[20]=*p1,*p1=*p2,*p2=p[20];
这句是不正确的,因为数组是不能直接赋值的,只能用strcpy函数来完成.
可更改如下:strcpy(p,p1);
strcpy(p1,p2);
strcpy(p2,p);
更改过后的整个程序如下:
#include
#include
void main()
{
char swap(char *p1,char *p2);
char n1[20],n2[20],n3[20];
char *pointer1,*pointer2,*pointer3;
scanf("%s",n1);
scanf("%s",n2);
scanf("%s",n3);
pointer1=n1;
pointer2=n2;
pointer3=n3;
if(strcmp(n1,n2)>0) swap(pointer1,pointer2);
if(strcmp(n1,n3)>0) swap(pointer1,pointer3);
if(strcmp(n2,n3)>0) swap(pointer2,pointer3);
printf("%s,%s,%s\n",n1,n2,n3);
}
char swap(char *p1,char *p2)
{
char p[20];
strcpy(p,p1);
strcpy(p1,p2);
strcpy(p2,p);
}