如何将值放入 const char **?

发布于 2024-07-11 17:02:44 字数 213 浏览 7 评论 0原文

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

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

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 **'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

合约呢 2024-07-18 17:02:44

哇,我很惊讶没有人得到这个! 也许我可以获得死灵法师徽章。 隐式 const 转换仅扫描一层深度。 因此,char* 可以变成 const char*,但它不会深入挖掘 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;
}

Wow, I'm surprised nobody got this! Maybe I can get a necromancer badge. Implicit const casting only scans one level deep. So a char* can become a const char* but it won't dig deep enough inside the a char** type to find what needs to be changed to make it 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;
}
陈独秀 2024-07-18 17:02:44

除了其他提到的,您可以将 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

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
朱染 2024-07-18 17:02:44

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

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.

秋千易 2024-07-18 17:02:44

您可以将 char ** 传递给声明为采用 const char ** 的函数 - 可能值得一看 MSDN 上的 const 文档

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

今天小雨转甜 2024-07-18 17:02:44

char ** 可以转换为 const char **,因此如果您想调用一个采用 const char * 的函数* 作为参数,只需提供您的 char ** 即可隐式转换。

如果您想编写一个函数,该函数采用const char **作为参数,然后修改它引用的char数据,那么您就破坏了与你的编译器的合同,即使你可以通过强制转换让它工作!

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!

余厌 2024-07-18 17:02:44

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


}

With my compiler (gcc version 3.4.4 in cygwin), I found that I could pass char * to const char *, but not char ** to const char **, unlike what most of the answers are saying.

Here is one way you can build something up that works; maybe it will help you.

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


}
真心难拥有 2024-07-18 17:02:44

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

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

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

you can un-const char* by using a cast operator: (char*)

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

use the same idea with the arrays char**

蓝戈者 2024-07-18 17:02:44

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 ** indicates that the underlying character is constant. So, while you can't do something like this:

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

but there is nothing preventing you from manipulating the pointer itself:

// 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");

That said, if you did want to modify the actual character data, you could use a non-const pointer and cast it:

char **foo = ...;
func((const char **)foo);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文