跳过正文

C/C++ const with Pointers

Tech C C++
浮世絵空事
作者
浮世絵空事
所谓灵魂 几行代码
    auto n = 1;
    auto m = 3;

    int* ptr = &m;

    const int* const p = &n; // const pointer, const data
    * p = 0;     // error
    p = ptr;    // error

    const int* q = &n; // non-const pointer, const data
    * q = 0;     // error
    q = ptr;

    int* const e = &n; // const pointer, non-const data
    * e = 0;
    e = ptr;    // error

    int* r = &n; // non-cosnt pointer, non-const data
    * r = 0;
    r = ptr;