c语言值传递和地址传递浅记

c语言值传递和地址传递浅记,第1张

 今天写冒泡排序打算用函数来实现两数值的交换,发现写的函数不起作用,经百度查找,应当使用地址传递。

#include
int c(int a,int b){
	int t;
	t=a;
	a=b;
	b=t;
	printf("%d,%d\n",a,b);
}
int main() {
	int x=1,y=2;
	c(x,y);
    printf("%d,%d",x,y);
	return 0;
}
/*
2,1
1,2
x和y的值没有改变
*/

#include
int c(int *a,int *b){
	int t;
	t=*a;
	*a=*b;
	*b=t;
	printf("%d,%d\n",*a,*b);
}
int main() {
	int x=1,y=2;
	c(&x,&y);
    printf("%d,%d",x,y);
	return 0;
}
/*
2,1
2,1
x和y的值发生改变
*/

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1325368.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-12
下一篇2022-06-12

发表评论

登录后才能评论

评论列表(0条)

    保存