c++ - 错误:“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";
}
解决方案
推荐阅读
- rest - 所有网站集中的 SharePoint 2013 REST API GetItems
- php - Laravel 在关联数组集合上的 contains() 助手,使用多个参数进行搜索
- android - Android 忽略大小写敏感的 Firebase whereEqualTo
- jquery - Typescript、JQuery、Angular 和“this”范围
- c++ - #define 来自另一个特定的翻译单元
- android - 改造 - 将请求正文作为数组或数字发送
- c++ - Print 语句不会出现在 C++ 游戏中
- maven - Gitlab - maven-surefire-plugin 的问题
- php - 自动计算动态行的产品总价
- matplotlib - 在pyplot中创建多个图形,但只显示一个