【发布时间】:2021-07-20 02:09:35
【问题描述】:
我很高兴在这里发布我的第一个问题。
所以我玩了一点指针来理解这个概念,我发现了这个错误
错误:从‘int*’到‘int’的无效转换[-fpermissive]
这里是代码:
#include <iostream>
using namespace std;
int main(){
int* pa,pb,pc,a,b,c;
pa = &a;
cin >> a;
cout <<"the value of a :"<<a<<endl;
cout <<"the value of pointer of a :"<<*pa<<endl;
// the problem begins when reading values of b :
pb = &b; //<== error
cin >> b;
cout << "the value of b : "<<b<<endl;
cout <<"the value of pointer of b" <<*pb<<endl;
return 0;
}
我不知道为什么它使用变量 a 成功但在 b 中使用相同的语法却失败了?
编辑:谢谢大家,我知道这个问题很简单,但我从你那里学到了:)
【问题讨论】:
-
b是什么类型?&b是什么类型?pb是什么类型的? -
有时在一行上定义多个变量是不值得的。
-
这导致了一个问题,为什么你会期望
pb是一个指针,但不期望b稍后在同一行。您可能会在自己探索这个问题时学到一些有用的东西。 -
每个声明只声明一个名称并在合理的情况下对其进行初始化的另一个原因。
-
第一个问题不错,顺便说一句。格式良好,易于阅读和理解。
标签: c++