首页 > 其他 > 详细

void swap(int a,int b)这样一个函数原型能交换两个数么?可以!!

时间:2014-12-11 22:24:44      阅读:315      评论:0      收藏:0      [点我收藏+]

昨天在指导别人指针的时候,突发奇想想到这么一道题,我觉得挺有意思的,发给大家看看,虽然不是什么很高级的技术,但是是个很有趣的思路.....

题目就是:

void swap(int a,int b)这个函数原型,不能用全局变量与静态变量的情况下,怎么实现交换两个数?

如果你有兴趣可以思考一下,如果没兴趣就直接看下面的答案吧。


--------------------------------------------------华丽的分割线------------------------------------------------------



#include "stdafx.h"
#include <iostream>

void swap(int a,int b)
{
	int c;
	c = *(int *)a;
	*(int *)a = *(int *)b;
	*(int *)b = c;
}

int main(int argc, char* argv[])
{
	int a = 2,b = 3;
	swap((int)&a,(int)&b);
	cout<<a<<b;
	system("pause");
	return 0;
}


void swap(int a,int b)这样一个函数原型能交换两个数么?可以!!

原文:http://blog.csdn.net/ganze_12345/article/details/41876261

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!