【发布时间】:2018-11-21 17:41:45
【问题描述】:
我想用指针交换两个整数的值。
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 **'
【问题讨论】:
-
你的编译器可能已经告诉你了。如果没有,请尝试启用警告消息。
-
是的,但为什么我不能做 temp = &a ?
-
因为它们是不同的数据类型
-
这使得
temp成为一个指向指针的指针,这是没有意义的。试试int temp=*a; *a=*b; *b=temp; -
啊,你这么说是因为a已经是一个指针了?