New to C++. Question about constant pointers

前端 未结 10 2088
梦谈多话
梦谈多话 2021-02-10 06:55

I am trying to learn C++ via some web tutorials. I don\'t have a compiler available to me, otherwise I would try this out. I\'m not sure what is meant by a const pointer. Doe

10条回答
  •  轮回少年
    2021-02-10 07:18

    A simple way to remember how const works with pointers is to remember that it always applies to whatever is to the left of it, unless it's the left-most keyword, in which case it applies to the right.

    Examples:

    Pointer to a constant char: The pointer can be changed to point to something else, but the char it initally points to cannot change value.

    const char * p;
    

    Constant pointer to a char: The pointer cannot be changed to point to anything else, but the char it points to can change value.

    char *const p;
    

    Constant pointer to a constant char: The pointer cannot be changed to point to anything else, and the char it points to cannot change value.

    const char *const p;
    

提交回复
热议问题