代码是否交换数组的两个数字

Does the code swap two numbers of the array?

本文关键字:两个 数字 是否 交换 数组 代码      更新时间:2023-10-16
实际

参数中应包含"&"以给出所需的结果,但以下代码也会正确交换数字。如何?

void swap(int *a, int *b){
    int t=*a;
    *a=*b;
    *b=t;
}
//there is an array ar[]
swap(ar[0],ar[1]);

它应该给出一些垃圾值,但它给出了正确的答案。

函数调用实际上可能是在调用库定义的std::swap函数,而不是自定义交换函数(因为类型不匹配(。

更改函数的名称,看看它是否可以编译。