Can we change the value of an object defined with const through pointers?

前端 未结 10 1324
北恋
北恋 2020-11-22 10:49
#include 
int main()
{
    const int a = 12;
    int *p;
    p = &a;
    *p = 70;
}

Will it work?

10条回答
  •  耶瑟儿~
    2020-11-22 11:21

    You cannot change the value of a constant variable by using a pointer pointing to it. This type of pointer is called as Pointer to a constant.

    There is also another concept called Constant Pointer. It means that once a pointer points to a memory location you cannot make it point to the another location.

提交回复
热议问题