为什么下面的代码编译:如何声明一个指针?
int main()
{
int j = 1;
int *jp = &j;
cout << "j is " << j << endl;
cout << "jp is " << jp << endl;
cout << "*jp is " << *jp << endl;
cout << "&j is " << &j << endl;
cout << "&jp is " << &jp << endl;
}
,但不为呢?
#include <iostream>
using namespace std;
int main()
{
int j = 1;
int *jp;
*jp =& j; // This is the only change I have made.
cout << "j is " << j << endl;
cout << "jp is " << jp << endl;
cout << "*jp is " << *jp << endl;
cout << "&j is " << &j << endl;
cout << "&jp is " << &jp << endl;
}
这个编译当我做jp = &j
,为什么?我只在另一行初始化了jp
,这对我没有意义。
你在编译时会得到什么错误? –
prog.cpp:9:错误:从'int *'无效转换为'int'结果:编译错误 – user1084113
感谢您的快速回复家伙,你的答案真的帮了大忙。 – user1084113