0

为什么我必须在使用指针时添加&incout子句。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)
}


I want my output would be something like this: stdout:

Element 0: 0x7fff1d26d6c0 + 0 bytes = 0x7fff1d26d6c0
Element 1: 0x7fff1d26d6c0 + 4 bytes = 0x7fff1d26d6c4
Element 2: 0x7fff1d26d6c0 + 8 bytes = 0x7fff1d26d6c8
Element 3: 0x7fff1d26d6c0 + 12 bytes = 0x7fff1d26d6cc
Element 4 : 0x7fff1d26d6c0 + 16 bytes = 0x7fff1d26d6d0
Element 5: 0x7fff1d26d6c0 + 20 bytes = 0x7fff1d26d6d4
Element 6: 0x7fff1d26d6c0 + 24 bytes = 0x7fff1d26d6d8
Element 7: 0x7fff1d26d6c0 + 28 bytes = 0x7fff1d26d6dc
Element 8: 0x7fff1d26d6c0 + 32 bytes = 0x7fff1d26d6e0

4

1 回答 1

1

关于什么

cout <<  bytes << " bytes" << " = " <<*(pMarks + i) << endl;

否则,您将传递来自 的地址pMarks + i

于 2013-02-27T15:07:27.217 回答