3

If I declare a variable const char ** stringTable, how should I be able to put values to it if it is a const? (It has to be const because a function I am supposed to use takes const char ** as a parameter.)

Edit: No you cannot convert from char ** to const char ** implicitly. Compiler complains: cannot convert parameter 3 from 'char **' to 'const char **'

4

8 回答 8

5

哇,我很惊讶没有人得到这个!也许我可以得到一个死灵法师徽章。隐式 const 转换仅扫描一层深度。所以 achar*可以变成 aconst char*但它不会深入到 achar**类型中以找到需要更改的内容以使其成为 a const char**

#include <iostream>
using 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;
}
于 2010-01-15T04:41:14.637 回答
4

Apart from other mentions that you can pass char** into function that takes const char **,

const char** is a non-const pointer to const char*, you can declare it and freely put values of type const char* in it.

On the other hand, you would not be able to do it, if you declared it as const char * const * or const char * const * const.

yourfunc(const char **p);
...
const char *array_str[10];
array_str[0] = "foo"; /* OK, literal is a const char[] */
yourfunc(array_str);

Here is what cdecl says:

cdecl> explain const char **table
declare table as pointer to pointer to const char
cdecl> explain const char * const *table
declare table as pointer to const pointer to const char
cdecl> explain const char * const * const table
declare table as const pointer to const pointer to const char
于 2009-01-23T09:40:57.010 回答
3

That const declaration is a quarantee of the function, you dont have to fullfill it. That means the function will keep your array untouched (it will just read). So you can pass a nonconst variable to a function expecting const.

于 2009-01-23T09:35:51.233 回答
3

You can pass a char ** to a function declared as taking a const char ** -- Might be worth taking a look at the documentation for const on MSDN

于 2009-01-23T09:37:37.657 回答
1

使用我的编译器(cygwin 中的 gcc 版本 3.4.4),我发现我可以传递char *const char *,但不能传递char **给,const char **这与大多数答案所说的不同。

这是一种可以构建有效的方法;也许它会帮助你。

void printstring( const char **s ) {
  printf( "%s\n", *s );
}

int main( int argc, char** argv ) {

  char *x = "foo";  // here you have a regular mutable string

  const char *x2 = x;  // you can convert that to a constant string

  const char **y = &x2;  // you can assign the address of the const char *

  printstring(y);


}
于 2009-01-23T22:56:33.877 回答
1

char ** can be converted to const char **, so if you want to call a function which takes a const char ** as a parameter, just supply your char ** and it'll be implicitly converted.

If you want to write a function which takes a const char ** as parameter and then modifies the char data it references, you're breaking the contract with your compiler, even if you might get it to work via casts!

于 2009-01-23T09:38:31.790 回答
0

您可以使用强制转换运算符取消 const char*: (char*)

void do_something(const char* s)
{
char* p=(char*)s;
p[0]='A';
}

对数组 char** 使用相同的想法

于 2009-01-23T23:02:40.857 回答
0

const char **表示底层字符是常量。所以,虽然你不能做这样的事情:

const char **foo = ...;
**foo = 'a';   // not legal

但是没有什么可以阻止您操纵指针本身:

// all the following is legal
const char **foo = 0;

foo = (const char **)calloc(10, sizeof(const char *));

foo[0] = strdup("foo");
foo[1] = strdup("baz");

也就是说,如果您确实想修改实际的字符数据,您可以使用非常量指针并将其转换:

char **foo = ...;
func((const char **)foo);
于 2010-01-15T05:29:24.900 回答