const and pointers in C

后端 未结 5 1985
星月不相逢
星月不相逢 2021-02-18 17:47

The use of const with a pointer can make the pointee not modifiable by dereferencing it using the pointer in question. But why neither can I modify what the pointer is not direc

5条回答
  •  说谎
    说谎 (楼主)
    2021-02-18 18:16

    Let's think about the type of the expressions.

    const int* ptr = &a;
    

    The type of ptr is const int*.

    So the type of *ptr is const int. Not modifiable.

    The type of (ptr + 2) is still const int* so the type of *(ptr + 2) is const int which is again not modifiable.

提交回复
热议问题