c - 交换函数和一般函数

标签 c swap

我正在尝试使用此交换功能碰运气,但遇到了问题。

我希望“新 num1”应该换成 num2 的值,反之亦然。

谁能把我推向正确的方向?

#include <stdio.h>


void swap(int *a, int *b)
{
int temp = *a;
a = *b;
b = temp;
printf("Just checking if this badboy gets to the swapfunction.\n");
}


int main()
{
int num1 = 33;
int num2 = 45;

swap(&num1, &num2);

printf("A: %d\n", num1);
printf("B: %d\n", num2);

getchar();
return 0;
}

最佳答案

您需要尊重指针:

int temp = *a;
*a = *b;
*b = temp;

你的编译器没有警告你吗?

关于c - 交换函数和一般函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39786864/

相关文章:

Android用动画交换两个图像

c++ - 不能在 C++ 中交换数组中的对象

rust - 为什么我得到 "cannot borrow ` arr[_ ]` as mutable more than once at a time"?

c - BCM2835 已安装但提供 undefined reference

java - 如何将数组列表中的特定项目移动到第一项

c++ - 3点之间的角度?

C 将配置文件绑定(bind)到可执行文件

python - 为什么当索引为 0 和 1 时交换不会发生?

c函数充当指针?

c - pthread_cond_broadcast 不向线程发送信号