猿问

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

#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;

}


ReZero
浏览 1759回答 2
2回答

流浪_老

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

流浪_老

#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;}
随时随地看视频慕课网APP
我要回答