首页 > 解决方案 > 无效的指针转换 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

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

标签: c++

解决方案


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

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

推荐阅读