为了账号安全,请及时绑定邮箱和手机立即绑定

函数参数,为什么第一次执行swap函数换了值却没执行函数swap中的打印

函数参数,为什么第一次执行swap函数换了值却没执行函数swap中的打印

C++
ReZero 2016-03-05 11:12:41
#include<cstdio>#include<iostream>using namespace std;int swap(int *a, int *b){    int t = *a;    *a = *b;    *b = t;    cout<<*a<<":"<<*b<<endl;}int main(){    int x1, y1;    cin>>x1>>y1;    swap(x1, y1);    cout<<x1<<" "<<y1<<endl;    int x2, y2;    cin>>x2>>y2;    swap(&x2, &y2);    cout<<x2<<" "<<y2<<endl;}
查看完整描述

2 回答

?
流浪_老

TA贡献71条经验 获得超14个赞

你第一次调用的时候没有加&符号哦

查看完整回答
1 反对 回复 2016-03-05
  • ReZero
    ReZero
    我知道,所以才问,没有加也能换
?
流浪_老

TA贡献71条经验 获得超14个赞

#include<cstdio>

#include<iostream>

using namespace std;

void swap(int *a, int *b){

    int t = *a;

    *a = *b;

    *b = t;

    cout<<*a<<":"<<*b<<endl;

}

int main()

{

    int x1, y1;

    cin>>x1>>y1;

    swap(&x1, &y1);

    cout<<x1<<" "<<y1<<endl;


    int x2, y2;

    cin>>x2>>y2;

    swap(&x2, &y2);

    cout<<x2<<" "<<y2<<endl;

}


查看完整回答
1 反对 回复 2016-03-05
  • 2 回答
  • 0 关注
  • 1730 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信