我无法理解一件奇怪的事。这是我的程序:
#include <iostream>
using namespace std;
void Swap(int *a , int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
int main()
{
int a=0;
int b=0;
cout<<"Please enter integer A: ";
cin>>a;
cout<<"Please enter integer B: ";
cin>>b;
cout<<endl;
cout<<"************Before Swap************\n";
cout<<"Value of A ="<<a<<endl;
cout<<"Value of B ="<<b<<endl;
Swap (&a , &b);
cout<<endl;
cout<<"************After Swap*************\n";
cout<<"Value of A ="<<a<<endl;
cout<<"Value of B ="<<b<<endl;
return 0;
}
现在,如果您查看函数“交换”,则我使用了“无效交换”。因此,它不能向主函数返回任何值(只有“ int”返回一个值(至少那是我的老师教我的))。但是,如果执行它,则将在主函数中交换这些值!怎么会 ?谁能告诉我这怎么可能?
实际上,该函数不返回值。它只是通过变量的地址访问值,并从它们的引用中交换它们。您的代码是正确的,现在我已经清除了您的概念,没有任何详细解释。就这样。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句