不知道这个程序是哪里出了问题

Rookie_Allen 2008-08-26 03:31:45
#include<stdio.h>
#include<string.h>
void main()
{
char *str1[20],*str2[20],*str3[20];
char swap();
printf("Input three lines:\n");
gets(str1);
gets(str2);
gets(str3);
if(strcmp(str1,str2)>0)swap(str1,str2);
if(strcmp(str1,str3)>0)swap(str1,str3);
if(strcmp(str2,str3)>0)swap(str2,str3);
printf("Now,the order is:\n");
printf("%s\n%s\n%s\n",str1,str2,str3);
}

char swap(char *p1,char *p2)
{
char *p[20];
strcpy(p,p1);strcpy(p1,p2);strcpy(p2,p);
}
...全文
80 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
hjzwl1018 2008-08-26
  • 打赏
  • 举报
回复

#include <stdio.h>
#include <string.h>

void swap(char *, char *); /*函数声明*/

void main()
{
char str1[20]={0},str2[20]={0},str3[20]={0}; /*字符数组就可以存放字符串了,干嘛还要定义字符指针数组?*/

printf("Input three lines:\n");
gets(str1);
gets(str2);
gets(str3);
if (strcmp(str1,str2)>0)
swap(str1,str2);
if (strcmp(str1,str3)>0)
swap(str1,str3);
if (strcmp(str2,str3)>0)
swap(str2,str3);
printf("Now,the order is:\n");
printf("%s\n%s\n%s\n",str1,str2,str3);
}

char swap(char *p1,char *p2) /*数组首地址自动退化成字符指针*/
{
char p[20]={0};

strcpy(p,p1);
strcpy(p1,p2);
strcpy(p2,p);
}
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 oo 的回复:]
把那些 * 去掉
[/Quote]
正解
taojian_hhu 2008-08-26
  • 打赏
  • 举报
回复
1,函数要先声明再使用。
2,gets参数类型不能为char**
3,swap和标准库函数重名
正确程序
#include <stdio.h>
#include <string.h>
void swap(char *p1,char *p2)
{
char p[20];
strcpy(p,p1);strcpy(p1,p2);strcpy(p2,p);
}
void main()
{
char str1[20],str2[20],str3[20];
char swap1();
printf("Input three lines:\n");
gets(str1);
gets(str2);
gets(str3);
if(strcmp(str1,str2)>0)
swap1(str1,str2);
if(strcmp(str1,str3)>0)
swap1(str1,str3);
if(strcmp(str2,str3)>0)
swap1(str2,str3);
printf("Now,the order is:\n");
printf("%s\n%s\n%s\n",str1,str2,str3);
}

lbh2001 2008-08-26
  • 打赏
  • 举报
回复
概念错误

#include <stdio.h>
#include <string.h>
int main(void)
{
char str1[20],str2[20],str3[20];
void swap();
printf("Input three lines:\n");
gets(str1);
gets(str2);
gets(str3);
if(strcmp(str1,str2)>0)swap(str1,str2);
if(strcmp(str1,str3)>0)swap(str1,str3);
if(strcmp(str2,str3)>0)swap(str2,str3);
printf("Now,the order is:\n");
printf("%s\n%s\n%s\n",str1,str2,str3);
return 0;
}

void swap(char *p1,char *p2)
{
char p[20];
strcpy(p,p1);
strcpy(p1,p2);
strcpy(p2,p);
}
xunfeng_2008 2008-08-26
  • 打赏
  • 举报
回复
把char *str1[20],*str2[20],*str3[20];
改成char str1[20],str2[20],str3[20];
就OK了

zjw6861982 2008-08-26
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <string.h>
void main()
{
char str1[20], str2[20], str3[20];
void swap();
printf("Input three lines:\n");
gets(str1);
gets(str2);
gets(str3);
if(strcmp(str1,str2)>0)
swap(str1,str2);
if(strcmp(str1,str3)>0)
swap(str1,str3);
if(strcmp(str2,str3)>0)
swap(str2,str3);
printf("Now,the order is:\n");
printf("%s\n%s\n%s\n",str1,str2,str3);
}

void swap(char *p1,char *p2)
{
char p[20];
strcpy(p,p1);
strcpy(p1,p2);
strcpy(p2,p);
}
god_sun 2008-08-26
  • 打赏
  • 举报
回复
太多错误了。
函数没声明,且定义的函数也错,返回值都没有。 定义数组错误。

oo 2008-08-26
  • 打赏
  • 举报
回复
把那些 * 去掉

70,022

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧