指针c ++解释

Geo*_*Lim 0 c++ pointers reference

我对这段代码感到困惑:

ipPtr = ipPtr + 3; // 5
cout << *ipPtr << endl; 
Run Code Online (Sandbox Code Playgroud)

为什么cout不是5而是一些随机大数?请有人向我解释.正如我的理解,我认为cout << *ipPtr << endl;是指出了*ipPtr上述情况.我对吗 ?

#include <iostream>

void main(){
    using namespace std;
    int iaArray[] = {1,2,3,4,5};
    int* ipPtr = 0;

    ipPtr = &(iaArray[1]);
    cout << *ipPtr << endl;//2

    ++ipPtr;
    cout << *ipPtr << endl;//3

    ipPtr = ipPtr + 3; //not 5 but random number. 
    cout << *ipPtr << endl; 
}
Run Code Online (Sandbox Code Playgroud)

Ed *_* S. 8

因为您已将指针递增超过数组的末尾.你似乎已经忘记了你++ipPtr在添加3之前写的.

          &(iaArray[1])
               |
iaArray = { 1, 2, 3, 4, 5 } ?
                  |         |
               ++ipPtr  ipPtr + 3
Run Code Online (Sandbox Code Playgroud)