No Indirection:

const char ch; // const character. must be initialized.
char const ch; // same as above
Single-Indirection:

char *p; // p is mutable, *p is mutable
const char *p; // p is mutable, *p is const
char const *p; // same as above.
char *const p; // p is const, *p is mutable, must be initialized.
char const *const p; // p is const, *p is const, must be initialized.
Double Indirection:

char **p; // ptr-to-ptr-to-char
// p, *p, and **p are ALL mutable

const char **p; // ptr-to-ptr-to-const-char
// p and *p are mutable, **p is const

char const **p; // same as above

char *const *p; // ptr-to-const-ptr-to-char
// p is mutable, *p is const, **p is mutable.

char **const p; // const-ptr-to-ptr-to-char
// p is const, *p is mutable, **p is mutable.
// must be initialized.

const char **const p; // const-ptr-to-ptr-to-const-char
// p is const, *p is mutable, **p is const.
// must be initialized.

char const **const p; // same as above

char const *const *p; // ptr-to-const-ptr-to-const-char
// p is mutable, *p is const, **p is const.

const char *const *p; // same as above.

char *const *const p; // const-ptr-to-const-ptr-to-char
// p is const, *p is const, **p is mutable.
// must be initialized.
And of course who can leave home without…

char const *const *const p; // const-ptr-to-const-ptr-to-const-char
// everything is const.
// must be initialized.

const char *const *const p; // same as above
So how does this affect your question? When compiling that code in C, without a cast you’ll get a compiler warning (or error if compiling with -Werror). When compiling in C++, you’ll just plain error because the parameter signature doesn’t match. But why?

Because these have no direct equivalence:

const char **p; // ptr-to-ptr-to-const-char
// p and *p are mutable **p is const

char **p; // ptr-to-ptr-to-char
// p, *p, and **p are all mutable

指针的指针与常量修饰符

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据