|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include<stdio.h>
void main()
{
int *p1,*p2,*p3,a,b,c;
int swap(int x,int y); //swap实现变换
printf("please input 3 numbers:\n");
scanf("%d%d%d",&a,&b,&c);
p1 = &a;
p2 = &b;
p3 = &c;
if(*p1 < *p2)
{
swap(*p1,*p2);
}
else if(*p2 < *p3)
{
swap(*p2,*p3);
}
else if(*p1 < *p2)
{
swap(*p1,*p2);
}
printf("%d >= %d >=%d \n",*p1,*p2,*p3);
}
int swap(int *p1,int *p2)
{
int t;
t = *p1;
*p1 = *p2;
*p2 = t;
printf("please wait");
}
D:\C\swap\1.c(26) : warning C4028: formal parameter 1 different from declaration
D:\C\swap\1.c(26) : warning C4028: formal parameter 2 different from declaration
D:\C\swap\1.c(32) : warning C4716: 'swap' : must return a value
Linking...
LINK : fatal error LNK1168: cannot open Debug/swap.exe for writing
执行 link.exe 时出错.
swap.exe - 1 error(s), 0 warning(s)
照着打的为啥会报错呢,我写的就是两个指针吧。显示的是不是使用和声明的形式不一样呀。
还有函数必须有返回值吗,我做一个两个数变换可不可以呢,求大神解答。感谢!
- #include<stdio.h>
- void swap(int *x,int *y); // 类型要一样,声明最好放外面
- int main(void) //建议改成 int,对调试有帮助
- {
- int *p1,*p2,*p3, a, b, c;
-
- printf("please input 3 numbers:\n");
- scanf("%d %d %d",&a,&b,&c); // 多重输入,最好有空格 %d %d 之间
-
- p1 = &a;
- p2 = &b;
- p3 = &c;
-
- if(*p1 < *p2)
- {
- swap(p1, p2); // 要求是指针类型,不需要加 *,加了变整型
- }
- else if(*p2 < *p3)
- {
- swap(p2, p3);
- }
- else if(*p1 < *p2)
- {
- swap(p1, p2);
- }
- printf("%d >= %d >=%d \n",*p1,*p2,*p3);
-
- return 0; // int 配合 return 0
- }
- void swap(int *p1,int *p2) // 类型要一样,压根没有 return 所以用 void
- {
- int t;
- t = *p1;
- *p1 = *p2;
- *p2 = t;
- printf("please wait");
- }
复制代码
你设计的程序还不完整,加油。输入 1 2 3 试试
|
|