首页 > 试题广场 >

试将下列程序中的指针改为引用: ===========

[问答题]
试将下列程序中的指针改为引用:
//===================================
#include<iostream>
using namespace std;
//-----------------------------------
void mySwap(int* a, int* b);
//-----------------------------------
int main(){
  int a=16, b=48;
  cout<<"a ="<<a<<",b= "<<b<<endl;
  mySwap(&a,&b);
  cout<<"After Being Swapped: \n";
  cout<<"a ="<<a<<", b="<<b<<endl;
}//----------------------------------
void mySwap(int* a, int* b);{
  int temp =*a;
  *a = *b;
  *b = temp;
}//==================================

推荐
//===================================
//EX0307.cpp
//-----------------------------------
#include<iostream>
using namespace std;
//-----------------------------------
void mySwap(int& a, int& b);
//-----------------------------------
int main(){
  int a=16, b=48;
  cout<<"a ="<<a<<",b= "<<b<<endl;
  mySwap(a, b);
  cout<<"After Being Swapped: \n";
  cout<<"a ="<<a<<", b="<<b<<endl;
}//----------------------------------
void mySwap(int& a, int& b);{
  int temp =a;
  a = b;
  b = temp;
}//==================================

发表于 2018-05-07 20:33:59 回复(0)