小编Ash*_*oss的帖子

交换整数的代码在 C++ 中有效,但在 C 中无效

我有这个交换通过引用传递的整数的函数,它在 C++ 中工作正常,但在 C 中不起作用。

#include <stdio.h>

void swap(int & x, int & y) 
{ 
    int z = x; 
    x = y; 
    y = z; 
}

int main() 
{
    int a = 0, b = 1;
    swap(a, b);
    printf("a is now %d\n", a);
    printf("b is now %d\n", b);
}
Run Code Online (Sandbox Code Playgroud)

为什么它在 C 中不起作用?

c c++ swap pass-by-reference

1
推荐指数
2
解决办法
402
查看次数

标签 统计

c ×1

c++ ×1

pass-by-reference ×1

swap ×1