C 如何将值放入常量字符**?

C 如何将值放入常量字符**?,c,char,constants,C,Char,Constants,如果我声明一个变量const char**stringTable,如果它是一个const,我应该如何给它赋值?(它必须是const,因为我应该使用的函数将const char**作为参数。) 编辑: 不,您不能隐式地从字符**转换为常量字符**。编译器抱怨: 无法将参数3从“char**”转换为“const char**”由于const声明是函数的保证,您不必填充它。这意味着该函数将保持数组不变(它将只是读取)。因此,您可以将非常量变量传递给需要常量的函数。您可以将char**传递给声明为使用c

如果我声明一个变量const char**stringTable,如果它是一个const,我应该如何给它赋值?(它必须是const,因为我应该使用的函数将const char**作为参数。)

编辑: 不,您不能隐式地从字符**转换为常量字符**。编译器抱怨:
无法将参数3从“char**”转换为“const char**”

由于const声明是函数的保证,您不必填充它。这意味着该函数将保持数组不变(它将只是读取)。因此,您可以将非常量变量传递给需要常量的函数。

您可以将
char**
传递给声明为使用
const char**
的函数——可能值得一看MSDN上的
char**
可以转换为
const char**
,因此,如果您想调用一个以
const char**
作为参数的函数,只需提供
char**
,它就会被隐式转换


如果您想编写一个以
const char**
为参数的函数,然后修改它引用的
char
数据,那么您就违反了与编译器的约定,即使您可能通过强制转换让它工作

除了提到可以将
char**
传递到接受
const char**
的函数之外

const char**
是指向
const char*
的非常量指针,您可以声明它并在其中自由放置
const char*
类型的值

另一方面,如果将其声明为
const char*const*
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);
以下是cdecl所说的:

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
使用我的编译器(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);


}

您可以使用cast运算符取消常量char*:(char*)


用同样的方法处理数组char**

哇,我很惊讶没有人得到这个!也许我能得到一个亡灵巫师徽章。隐式常量投射只扫描一层深度。因此,
char*
可以变成
const char*
,但它在a
char**
类型中挖掘的深度不够,无法找到需要更改哪些内容才能使其成为
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;
}
#包括
使用名称空间std;
无效打印3(常量字符**3){
对于(int x=0;x<3;++x){

cerr
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);

您可以更改指针,但不能更改指针指向的数据。例如:const char**c;const char*str=“我的字符串”;c=&str;//legalaadditional更改const char**指向ie的指针是合法的:*c=str//legal您可以将
char*
传递给期望
const char*
的函数。您不能将
char**
作为
const char**
传递。这不是一个很大的问题,隐式强制转换对于simplic来说只在一个层次上起作用城市。
char*x=“foo”
不应该编译,因为“foo”是
const char*
。没有必要使用
x2
,因为你可以将
char*
分配到
const char**
数组中。我也很惊讶没有其他人得到这个,干得好,先生:)
char **foo = ...;
func((const char **)foo);