如果我声明一个变量const char**stringTable,如果它是一个const,我怎么能把值放到它?(它必须是const,因为我应该使用的函数将const char**作为参数.)
编辑:不,你不能隐式地从char**转换为const char**.编译器抱怨:无法将参数3从'char**'转换为'const char**'
哇,我很惊讶没有人得到这个!也许我可以获得死灵法师徽章.隐式const转换仅扫描一层深度.所以a char*
可以成为一个const char*
但是它不会在一个char**
类型内深入挖掘以找到需要改变的东西来使它成为一个const char**
.
#includeusing namespace std; void print3( const char **three ) { for ( int x = 0; x < 3; ++ x ) { cerr << three[x]; } } int main() { // "three" holds pointers to chars that can't be changed const char **three = (const char**) malloc( sizeof( char** ) * 3 ); char a[5], b[5], c[5]; // strings on the stack can be changed strcpy( a, "abc" ); // copy const string into non-const string strcpy( b, "def" ); strcpy( c, "efg" ); three[0] = a; // ok: we won't change a through three three[1] = b; // and the (char*) to (const char*) conversion three[2] = c; // is just one level deep print3( three ); // print3 gets the type it wants cerr << endl; return 0; }