首页 > 编程语言 > 详细

C++入门经典-例5.7-调用自定义函数交换两变量值,传入指针

时间:2017-09-14 19:56:12      阅读:326      评论:0      收藏:0      [点我收藏+]

1:代码如下:

技术分享
// 5.7.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
using namespace std;
void swap(int *a,int *b)
{
    int tmp;
    tmp=*a;
    *a=*b;
    *b=tmp;
}
void swap(int a,int b)
    {
    int tmp;
    tmp=a;
    a=b;
    b=tmp;
}
void main()
{
    int x,y;
    int *p_x,*p_y;
    cout << " input two number " << endl;
    cin >> x;
    cin >> y;
    p_x=&x;p_y=&y;
    cout<<"按指针传递参数交换"<<endl;
    swap(p_x,p_y);//执行的是参数列表都为指针的swap函数
    cout << "x=" << x <<endl;
    cout << "y=" << y <<endl;
    cout<<"按值传递参数交换"<<endl;
    swap(x,y);
    cout << "x=" << x <<endl;
    cout << "y=" << y <<endl;
}
View Code

运行结果:

技术分享

C++入门经典-例5.7-调用自定义函数交换两变量值,传入指针

原文:http://www.cnblogs.com/lovemi93/p/7522051.html

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