无效指针转换C++

我很高兴在这里发布我的第一个问题。

所以我玩了一点点来理解这个概念,我发现了这个错误

错误:从“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

编辑:谢谢大家,我知道这个问题很简单,但我从你那里学到了:)

回答

*绑定到变量名称,而不是类型。所以你真正想要的是:

int *pa,*pb,*pc,a,b,c;


回答

在声明中

int* pa,pb,pc,a,b,c;

pa声明为int*. 其他变量声明为int.

您需要将变量声明为

int *pa, *pb, *pc, a, b, c;


以上是无效指针转换C++的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>