簡體   English   中英

C ++增加指向數組的指針

[英]C++ incrementing a pointer to an array

int arr[10] = {1,2,3,4,5,6,7,8,9,10};
int (*parr)[10] = &arr;

//prints address of arr and the value 1
cout << parr << " " << *parr[0];

//what is this doing?
parr++;

//prints (what looks like the address of arr[1]) and some long number -8589329222
cout << parr << " " << *parr[0]; 

我以為parr ++會增加parr指向的地址,因此* parr [0]現在是* parr [1]的地址。 我哪里錯了?

您假設parr++遞增一個單詞。 沒有。 它以*parr的大小遞增,在這種情況下為int[10] ,因此它以10個整數(可能為40個字節)的大小遞增。

您只需要一個指向數組開頭的指針。

int* parr = arr; // points to the 0 element
parr++; // poInts to the first element, 1.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM