习题6.10:编写一个函数,使用指针形参交换两个整数的值。

void swap(int* ip1, int* ip2){
	int temp;
	temp = *ip1;
	*ip1 = *ip2;
	*ip2 = temp;

	
}

int main()
{
	int a=5, b=7;
	swap(&a,&b);
	cout<<"a="<<a<<" "<<"b="<<b<<endl;
	return 0;
}

传引用类型的

void swap(int &ival1, int &ival2){
	int temp;
	temp = ival1;
	ival1 = ival2;
	ival2 = temp;

	
}

int main()
{
	int a=5, b=7;
	swap(a,b);
	cout<<"a="<<a<<" "<<"b="<<b<<endl;
	return 0;
}

    原文作者:zyn好好学习
    原文地址: https://blog.csdn.net/sinat_33531884/article/details/121320278
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞