#include<iostream>
using namespace std;

void swap(int* p1, int* p2) {  //swap函数的形参接收解引用地址 p1和p2
    int temp = *p1;
    *p1 = *p2;
    *p2 = temp;
}

int main() {

    int a = 10;
    int b = 20;
    swap(&a, &b);   //调用者传入的实参为内存地址,这将改变实参int a和int b的值
    cout << "int a = " << a <<"\t int b = " << b << endl;
}

发表回复

后才能评论