在这个程序中,我创建了两个指针(a,b),它们指向x和y的内存地址。在函数中,我创建了一个交换a和b的内存地址的操作(所以b=a和a=b)。但是编译时出现了错误(invalid conversion from 'int' to 'int*'),这是什么意思呢?我是把指针传递到函数中还是将它作为普通的int类型读取了呢?
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
void pointer(int* x,int* y)// Swaps the memory address to a,b
{
int *c;
*c = *x;
*x = *y;
*y = *c;
}
int main()
{
int x,y;
int* a = &x;
int* b = &y;
cout<< "Adress of a: "<<a<<" Adress of b: "<<b<<endl; // Display both memory address
pointer(*a,*b);
cout<< "Adress of a: "<<a<<" Adress of b: "<<b<<endl; // Displays the swap of memory address
return 0;
}
错误信息:
C++.cpp:在函数'main()'中:
C++.cpp:20:16:错误:从'int'到'int*'的转换无效[-fpermissive]
C++.cpp:6:6:错误:初始化'void pointer(int *,int *)'的参数1[-fpermissive]
C++.cpp:20:16:错误:从'int'到'int*'的转换无效[-fpermissive]
C++.cpp:6:6:错误:初始化'void pointer(int *,int *)'的参数2[-fpermissive]
a
的任何指针在交换之后将指向b
。因此它实际上具有与交换值相同的效果,或者是我太困惑了? - 463035818_is_not_a_number