为什么const指针中的结构操作是非法的
Why illegal structure operation in const pointer?
为什么我必须在cout
子句中添加&
,而使用const
指针。我在下面的代码中。如果我不加&
子句,它就是illegal structure operation
。
int Marks [10]= {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
// Create a constant pointer to Marks array
const int* pMarks = Marks;
for (int i = 0, bytes = 0; i < 10; ++i, bytes += 4)
{
cout << "Element " << i << ": " << pMarks <<" + ";
cout << bytes << " bytes" << " = " << (pMarks + i) << endl; // that & is required before (pMarks + i)
}
我希望我的输出是这样的:stdout:
元素0:0x7fff1d26d6c0 + 0字节= 0x7fff1d26d6c0
元素1:0x7fff1d26d6c0 + 4字节= 0x7fff1d26d6c4
元素2:0x7fff1d26d6c0 + 8字节= 0x7fff1d26d6c8
元素3:0x7fff1d26d6cc + 12 bytes = 0x7fff1d26d6cc
元素4:0x7fff1d26d6c0 + 16字节= 0x7fff1d26d6d0
元素5:0x7fff1d26d6c0 + 20 bytes = 0x7fff1d26d6d4
元素6:0x7fff1d26d6c0 + 24字节= 0x7fff1d26d6d8
元素7:0x7fff1d26d6c0 + 28字节= 0x7fff1d26d6dc
元素8:0x7fff1d26d6c0 + 32 bytes = 0x7fff1d26d6e0
cout << bytes << " bytes" << " = " <<*(pMarks + i) << endl;
否则,您将从pMarks + i
传递地址
相关文章:
- 为什么在popback()操作之后,它仍然打印完整的矢量
- 无法编译 rtmidi 测试 cmidiin.cpp 文件, 非法指令
- 重载操作程序时出错>>用于类中的字符串 memebr
- 对字符串进行位操作
- 我可以在 C++ 中的函数体之外进行操作吗?
- MPI突然停止了对多个核心的操作
- 如何在信号处理程序和普通函数中对全局变量进行互斥读写操作
- 对字符数组中的元素执行逐位操作
- 如何在directx/c++中进行平移/缩放操作
- 逐位操作的隐式类型转换
- 为什么一个向量上的多线程操作很慢
- 编写代码时C++出现错误:错误 1 错误 C2601:'circle':本地函数定义是非法的
- 排序时无法执行交换操作.我做的时候它会崩溃.为什么
- 位移操作和位掩码未检测到重复字符
- C2276 '&':对绑定成员函数表达式的非法操作
- 记事本++中的非法操作
- '&' :对绑定成员函数表达式的非法操作
- "&":对绑定成员函数表达式的非法操作。从关键点矢量 OpenCV 获取容量
- "!"绑定成员函数表达式的非法操作
- 为什么const指针中的结构操作是非法的