代码问题
#include <stdio.h>int main()
{
int a,b,c,t;
int *pa,*pb,*pc;
printf("请输入三个数字:");
scanf("%d%d%d",&a,&b,&c);
*pa =&a;
*pb =&b;
*pc =&c;
if(a > b)
{
t = *pa;
*pa = *pb;
*pb = t;
}
if (a > c)
{
t = *pa;
*pa = *pc;
*pc = t;
}
if(b>c)
{
t = *pb;
*pb =*pc;
*pc = t;
}
printf("%d <= %d <= %d",*pa,*pb,*pc);
getchar();getchar();
return 0;
}
这个程序运行出错。然后退出程序之后让我调试。 改成这样:
#include <stdio.h>
int main()
{
int a,b,c,t;
int *pa,*pb,*pc;
printf("请输入三个数字:");
scanf("%d %d %d",&a,&b,&c);
pa =&a;
pb =&b;
pc =&c;
if(a > b)
{
t = *pa;
*pa = *pb;
*pb = t;
}
if (a > c)
{
t = *pa;
*pa = *pc;
*pc = t;
}
if(b>c)
{
t = *pb;
*pb =*pc;
*pc = t;
}
printf("%d <= %d <= %d",*pa,*pb,*pc);
getchar();getchar();
return 0;
} *pa =&a;
*pb =&b;
*pc =&c;
应该改成这样:
pa = & a;
pb = & b;
pc = & c; jackz007 发表于 2020-4-29 17:51
应该改成这样:
好的,谢谢。 这里是给 pa、pb、pc 的地址赋值,不是给 pa、pb、pc 指向的值赋值,所以不用加星号。
帮你改好了并且规范了一下代码:
#include <stdio.h>
int main()
{
int a, b, c, t;
int *pa, *pb, *pc;
printf("请输入三个数字:");
scanf("%d%d%d", &a, &b, &c);
pa = &a;
pb = &b;
pc = &c;
if (a > b)
{
t = *pa;
*pa = *pb;
*pb = t;
}
if (a > c)
{
t = *pa;
*pa = *pc;
*pc = t;
}
if (b > c)
{
t = *pb;
*pb = *pc;
*pc = t;
}
printf("%d <= %d <= %d", *pa, *pb, *pc);
getchar();
getchar();
return 0;
} zltzlt 发表于 2020-4-29 17:55
这里是给 pa、pb、pc 的地址赋值,不是给 pa、pb、pc 指向的值赋值,所以不用加星号。
帮你改好了并且规 ...
看到了看到了,谢谢{:10_297:}
页:
[1]