我想使用指针交换2个整数的值。
void swap(int *a, int *b)
{
int *temp;
temp = &a;
*a = *b;
*b = *temp;
}
为什么这不起作用?
给出错误:
incompatible pointer types initializing 'int *' with an expression of type 'int **'
答案 0 :(得分:3)
temp
是一个指针。您想要的是int
取a
所指向的值。您需要将temp
更改为int
。
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = *temp;
}
您可能需要按照C的一些教程来理解指针的工作方式。对于Stack Overflow答案以及在线和书籍中提供的大量可用资源,要进行大量解释。