C++:const char*const* 的含义是什么?

28
在一个C++程序中,我看到了一个函数原型:int Classifier::command(int argc, const char*const* argv) const char*const* argv是什么意思?它和const char* argv[]是一样的吗? const char** argv也是指的同样的内容吗?

9
http://cdecl.org/ - Luchian Grigore
3
指向常量指针的指针,主要从右向左阅读。 - Peter Wood
5个回答


10

不,它与const char *argv[]并不相同。 const禁止在特定的解引用级别上修改解引用的值:

**argv = x; // not allowed because of the first const
*argv = y; // not allowed because of the second const
argv = z; // allowed because no const appears right next to the argv identifier

4

const char*const* argv 表示 "指向常量指针的常量指针,指向常量 char"。它并不是与 const char *argv[] 相同,但在某种程度上是兼容的:

void foo(const char *const *argv);

void bar(const char **argv)
{
    foo(argv);
}

可以成功编译。(如果没有const_cast,反过来就无法编译。)


2
一个不变的指针指向一个不变的字符串:
const char* aString ="testString";

aString[0] = 'x';   // invaliv since the content is const
aString = "anotherTestString"; //ok, since th content doesn't change

const char const* bString = "testString";
bString [0] = 'x'; still invalid
bString = "yet another string"; // now invalid since the pointer now too is const and may not be changed.

const char const* bString = "testString"; is the same thing as const char* aString ="testString";. If you wanted your second example to be correct, you would put the const after the * like so: const char *const aString ="testString"; - S.S. Anne

0
const char*const* a;

基本上说: a 是一个指向不可更改的常量字符指针。
因此,以下代码是有效的:
const const const char*const*const* a;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接