Why does this allow promotion from (char *) to (const char *)?

后端 未结 4 446
野性不改
野性不改 2021-01-13 18:53

Given that scanf has (const char *) in the documentation from Microsoft and the answer to this question what the heck is going when I do the same for (char **) promotion to

4条回答
  •  佛祖请我去吃肉
    2021-01-13 19:42

    You're allowed to increase access restriction, you just can't decrease it. Going from a normal pointer to a const pointer is fine, going from a const pointer to a normal pointer is not.

    The second example doesn't compile because you're not converting a pointer to a const pointer, you're converting from a pointer to one type (char*) to another (const char*). For example, you can change a char** to a char* const*, but not a const char**.

提交回复
热议问题