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

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

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

    This is a pointer (that you can change) to a char (or multiple chars) that you cannot change. In other words, all string literals.

    (3) const char const*
    

    This is doubled up. I think you were trying to go for the third position:

    (4) const char * const
    

    which is a pointer that you cannot change, to a char (or multiple chars) that you cannot change. You can use this for global pointers to literals that should not be accidentally changed.

    The string literals are going to be (most likely) lumped right after your code, usually in a segment or section called "rodata".

提交回复
热议问题