Difference between const char*, char const*, const char const* & string storage

后端 未结 6 1779
后悔当初
后悔当初 2020-12-24 15:50

First of all, what\'s the difference between:

(1) const char*
(2) char const*
(3) const char const*

I\'m fairly certain I understand this f

6条回答
  •  时光说笑
    2020-12-24 16:19

    There are quite a lot correct answers here but you may find it hard to remember, here is a trick to memorize this :

    1> when const is on the left hand side of *, it means the pointer points to a constant object;

    e.g.const int * p means the int cannot be changed via pointer p

    2> when const is on the right hand side of *, it means the pointer is a const pointer;

    e.g.int * const p means p is a constant pointer which cannot be changed.

    BTW if you have const on both side of *, then it means it's a const pointer and you cannot change the object via the pointer.

    e.g. int const * const p

提交回复
热议问题