首页 > 解决方案 > 错误:“int*”和“int”类型的无效操作数到二进制“operator*”

问题描述

在这段代码中,我可以理解处理“a * 2”的注释代码发生错误,因为“a”是数组的地址。所以可以修改。但我不明白为什么第 32 行和第 33 行会出错。我想当我创建另一个名为'b'的指针变量时,我可以修改变量'b'。但这让我犯了错误。请告诉我为什么会发生错误......

#include <iostream>

using namespace std;

/*
 * This is an example about pointer arithemtic
 */
int main()
{
    int a[5];

    for (int i = 0; i < 5; i++) {
        a[i] = i;
    }

    // pointer arithemtic for +
    for (int i = 0; i < 5; i++) {
        cout << a + i << "\n";
    }

    // pointer arithemtic for -
    int *c = a;
    for (int i = 1; i < 5; i++) {
        cout << a + i - c << "\n";
    }

    /* Try to compile the folloing region */
//     cout << a * 2 << "\n";
//     cout << a / 2 << "\n";
//
     int *b = a;
     cout << b * 2 << "\n";
     cout << b / 2 << "\n";
}

标签: c++arrayspointers

解决方案


推荐阅读