C++ 初始化字符**

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18869282/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:16:18  来源:igfitidea点击:

Initialize char**

c++string

提问by Boyang

I'm very new to C++. I'm trying to call a function that takes in char**:

我对 C++ 很陌生。我正在尝试调用一个接受 char** 的函数:

bool func(char** a) {
    //blablabla
}

So it takes in an array of c-strings. I need to create a char**, but nothing works.

所以它需要一个 c 字符串数组。我需要创建一个字符**,但没有任何效果。

char** a = char[255][255]; // error: type name is not allowed

char** a = new char[255][255]; // error: a value of type "char (*)[255]" cannot be used to initialize an entity of type "char **"

char a[][] = {"banana", "apple"};
char** b = &a; // error: a value of type "<error-type> (*)[2]" cannot be used to initialize an entity of type "char **"

At the end I need to do:

最后我需要做的是:

char* a[] = {"banana", "apple"};

Why the first few didn't work and why the last one worked?

为什么前几个不起作用,为什么最后一个起作用?

Thanks in advance.

提前致谢。

采纳答案by Shoe

There's a lot wrong in your code.

你的代码有很多错误。

char** a = char[255][255]; // error: type name is not allowed

First of all this is not even valid C++ (or C for that matter). Maybe you meant:

首先,这甚至不是有效的 C++(或 C)。也许你的意思是:

char a[255][255];

In any case always remember that the type of a bi-dimensional dynamically allocated array is not **but (*)[N]which is very different.

在任何情况下都要记住,二维动态分配数组的类型不是**但这(*)[N]是非常不同的。

char** a = new char[255][255]; // error: a value of type "char (*)[255]" cannot be used to initialize an entity of type "char **"

The error message you provide in the comment explains exactly what I said earlier.

您在评论中提供的错误消息完全解释了我之前所说的内容。

char a[][] = {"banana", "apple"};

In the above code the correct type of the variable ashould be char* a[]. Again, arrays and pointer (for what the type is concerned) are very different things. A chararray may decay to pointer (if NULL?terminated), but for the rest, except with explicit casts, you can't use pointers and arrays like you are doing.

在上面的代码中,变量的正确类型a应该是char* a[]. 同样,数组和指针(对于类型而言)是非常不同的东西。一个char阵列可以衰减到指针(如果NULL?终止),但其余,除具有显式转换,则不能使用指针和数组一样,你在做什么。

The last one worked because, like I said earlier, char* []is the correct type for an array of C-strings.

最后一个有效,因为正如我之前所说,它char* []是 C 字符串数组的正确类型。

Anyway, if you just doing homework, it is ok to learn this things. But in future development using C++: try not to use "features" that start with C-, like C-strings, C-arrays, etc. C++'s standard library gives you std::string, std::array, std::vectorand such for free.

不管怎样,如果你只是做功课,学习这些东西是可以的。但在使用C ++未来的发展:尽量不要使用与启动“特色” C-,像C字符串,C-阵列等C ++标准库给你std::stringstd::arraystd::vector和这样的免费

If you reallyneed to allocate dynamic memory (with newand delete, or new[]and delete[]) please use smart pointers, like std::shared_ptror std::unique_ptr.

如果您确实需要分配动态内存(使用newanddeletenew[]and delete[]),请使用智能指针,例如std::shared_ptror std::unique_ptr

回答by Hyman

You say you are working in C++. Then you can easily ignore const char*and char**and focus about what you can use:

你说你正在使用 C++。然后,您可以轻松忽略const char*char**专注于您可以使用的内容:

#include <string>
#include <vector>

std::vector<std::string> arrayOfStrings;
arrayOfStrings.push_back("foo");

bool func(const std::vector<std::string>>& a) {
  ..
}

If you know the size at compile time you can even use std::array:

如果您知道编译时的大小,您甚至可以使用std::array

std::array<255, std::string> fixedArrayOfStrings

EDIT: since you need to build an array of C strings in any case you can easily do it starting from the vector:

编辑:因为您需要在任何情况下构建一个 C 字符串数组,您可以从向量开始轻松地完成它:

const char **arrayOfCstrings = new const char*[vector.size()];

for (int i = 0; i < vector.size(); ++i)
  arrayOfCstrings[i] = vector[i].c_str();

func(arrayOfCstrings);

delete [] arrayOfCstrings;

回答by Zaffy

char**

is ambiguous - it can mean:

不明确 - 它可能意味着:

  1. pointer to pointer
  2. array of c-strings - experienced programmer would write char* arr[]instead
  1. 指向指针的指针
  2. C-字符串数组-有经验的程序员会写char* arr[],而不是

In the first case it is quite simple:

在第一种情况下,它非常简单:

char* niceString = GetNiceString();
func(&niceString);

however in the second case it is slightly more complex. The function will not know the length of the array so you need to end it explicitly with a NULL, just like for example environis:

然而,在第二种情况下,它稍微复杂一些。该函数不知道数组的长度,因此您需要以 a 显式结束它NULL,例如environ

char* a[3] = { "One", "Two", NULL }; /* note that this is possibly  dangerous
because you assign const char* (READ-ONLY) to char* (WRITABLE) */
func(a); // char*[] gets downgraded to char** implicitly

回答by dare

so char** a = char[255][255];it's weird to C and C++ and if you want a static 2d array just

所以char** a = char[255][255];这对 C 和 C++ 来说很奇怪,如果你想要一个静态的二维数组

char a[255][255];