当前位置: 代码迷 >> 综合 >> func(int a,int b)和func(int *a,int *b)
  详细解决方案

func(int a,int b)和func(int *a,int *b)

热度:9   发布时间:2023-12-11 20:41:47.0
#include<iostream>
using namespace std;void exchange(int *a,int *b)
{
    int tmp;tmp=*b;*b=*a;*a=tmp;
}void exchange_error(int a,int b)
{
    int tmp;tmp=b;b=a;a=tmp;
}
int main()
{
    int a=4;int b=3;exchange(&a,&b);//这里传递的是指针,直接对a和b的地址操作cout<<"a="<<a<<" b= "<<b<<endl;exchange_error(a,b);//这个只是将a=4和a=3传递给exchange_error中的局部变量a和b,// exchange_error运行完局部变量的栈就被系统回收了cout<<"a="<<a<<" b= "<<b<<endl;system("pause");return 0;
}
  相关解决方案