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

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

What\'s the difference between:

char * const 

and

const char *
19条回答
  •  猫巷女王i
    2020-11-22 03:42

    1. const char* x Here X is basically a character pointer which is pointing to a constant value

    2. char* const x is refer to character pointer which is constant, but the location it is pointing can be change.

    3. const char* const x is combination to 1 and 2, means it is a constant character pointer which is pointing to constant value.

    4. const *char x will cause a compiler error. it can not be declared.

    5. char const * x is equal to point 1.

    the rule of thumb is if const is with var name then the pointer will be constant but the pointing location can be changed , else pointer will point to a constant location and pointer can point to another location but the pointing location content can not be change.

提交回复
热议问题