帮我详细解下这个C语言题吧?

来源:百度知道 编辑:UC知道 时间:2024/05/23 13:26:23
#include<stdio.h>
void main ( )
{int a=-1,b=-1;
void f1(int x,int y),f2(int *x,int *y);
void f3(int *x,int *y),f4 (int x,int y);
f1 (a,b);
printf("(%d,%d)\n",a,b);
a=-1,b=-1;
f2(&a,&b);
printf("(%d,%d)\n",a,b);
a=-1,b=-1;
f3(&a,&b);
print,%df("(%d)\n",a,b);
a=-1,b=-1;
f4(&a,&b);
print,%df("(%d)\n",a,b);
}
void f1( int x,int y)
{int t;
t=x;x=y;y=t;
}
void f2 (int *x,int *y)
{int t;
t=*x;*x=*y;*y=t;
}
void f3(int *x,int *y)
{t=x;x=y;y=t;
}
void f4(int x,int y)
{int *t=(int *)malloc(sizeof(int));
*t=x;x=y;y=*t;
}

程序运行时,第1行,第2行,第3行,第4行,分别输出什么?
请各位指教了,最好越详细越好,小女子在此谢过了,
急!
哦,是失误啊,a=-1,b=1 ;当时写的时候没注意啊,那你能不能帮我详细解答一下呢,看你是很懂的,真是谢谢哦

写程序极不规范,跟我初学时一样,你把两个数设置成一样的,怎么知道有没有交换数据,我给你改了一下
主要的考点是用指针传递参数的应用

#include<stdio.h>
#include<stdlib.h>

void main ( )
{
int a=2,b=3;
void f1(int x,int y),f2(int *x,int *y);
void f3(int *x,int *y),f4 (int x,int y);
f1 (a,b);
printf("(%d,%d)\n",a,b);
a=2,b=3;
f2(&a,&b);
printf("(%d,%d)\n",a,b);
a=2,b=3;
f3(&a,&b);
printf("(%d,%d)\n",a,b);
a=2,b=3;
f4(a,b);
printf("(%d,%d)\n",a,b);
}
void f1( int x,int y) //传递的变量的值,不会改变原数的值
{
int t;
t=x;x=y;y=t;
}
void f2 (int *x,int *y) //传递的是地址,改变变量的值,会改变原来的值
{
int t;
t=*x;*x=*y;*y=t;
}
void f3(int *x,int *y) //传递的是地址,但改变的是变量地址的值,没有改变变量的值,不会改变原数的值
{