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

后端 未结 6 1810
后悔当初
后悔当初 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:13

    • const char * and char const * have the same meaning: the pointed value is const, but the pointer itself can be changed to another address. After initialization, *p = 'X'; is invalid (does not compile), and p = &x; (where x is a variable of type char) is valid (it compiles).

    • So const char const * indicates twice the same thing.

    • char * const: the pointed value can be changed, but the pointer itself is const. It cannot be modified to point to another address. After initialization, *p = 'X'; is valid, and p = &x; is not.

    • const char * const and char const * const have the same meaning: both pointed value and pointer are const. Neither *p = 'X'; nor p = &x; are valid after initialization.

提交回复
热议问题