What is the difference between char * const and const char *?

前端 未结 19 1482
执笔经年
执笔经年 2020-11-22 03:31

What\'s the difference between:

char * const 

and

const char *
19条回答
  •  没有蜡笔的小新
    2020-11-22 03:52

    Lots of answer provide specific techniques, rule of thumbs etc to understand this particular instance of variable declaration. But there is a generic technique of understand any declaration:

    Clockwise/Spiral Rule

    A)

    const char *a;
    

    As per the clockwise/spiral rule a is pointer to character that is constant. Which means character is constant but the pointer can change. i.e. a = "other string"; is fine but a[2] = 'c'; will fail to compile

    B)

    char * const a;
    

    As per the rule, a is const pointer to a character. i.e. You can do a[2] = 'c'; but you cannot do a = "other string";

提交回复
热议问题