我正在尝试使用此交换功能碰运气,但遇到了问题。
我希望“新 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/