0

我正在尝试创建一个 char**,其中包含我从文件中读取的一些字符串,但是当我尝试创建它时,我收到一条错误消息:

error C2440: 'initializing' : cannot convert from 'char ***' to 'char **'

`

Portion of code with the error:

//limit defined above this
char** re = new (char**)[limit]; <---------- Error
for(int x = 0; x<limit;x++) {
    re[x]=(char*)stringsfromfile[x].c_str();
}

我写 c++ 的时间很短,我不明白为什么会出现这个错误。在互联网上搜索了几个小时的答案,但仍然找不到任何东西。如果我不清楚我道歉,请告诉我我一直不清楚的地方。

谢谢!

4

2 回答 2

3

您正在尝试创建一个 char 指针数组,这意味着您应该使用:

char** re = new (char*)[limit];

但是,您可能需要考虑采用 C++ 的一些非 C 方面,例如向量 - 您会发现您的工作效率大大提高。

于 2013-08-29T04:00:54.967 回答
0

左手操作与右手操作不匹配。

  • 左手:char 双指针
    右手:数组的 char 双指针(产生第三个指针)。

所以如果你想使用数组,你可以像下面给出的例子一样使用:

char **re = (char *[]){ "New Game", "Continue Game", "Exit" };

但是,选择只能用于线性寻址。例如:

printf ("%s", &(*re)[0]); outputs: New Game
printf ("%s", &(*re)[1]); outputs: ew Game
printf ("%s", &(*re)[9]); outputs: Continue Game
于 2013-08-29T04:08:16.990 回答