C++ 初始化一个 Char*[]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2240094/
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
Initializing a Char*[]
提问by DogDog
Question is in the title, how do I initialize a char*[] and give values to it in C++, thank you.
问题在标题中,我如何初始化 char*[] 并在 C++ 中为其赋值,谢谢。
采纳答案by Stephen Cross
Though you're probably aware, char*[] is an array of pointers to characters, and I would guess you want to store a number of strings. Initializing an array of such pointers is as simple as:
虽然您可能知道, char*[] 是一个指向字符的指针数组,我猜您想存储许多字符串。初始化此类指针的数组非常简单:
char ** array = new char *[SIZE];
...or if you're allocating memory on the stack:
...或者如果您在堆栈上分配内存:
char * array[SIZE];
You would then probably want to fill the array with a loop such as:
然后,您可能希望使用循环填充数组,例如:
for(unsigned int i = 0; i < SIZE; i++){
// str is likely to be an array of characters
array[i] = str;
}
As noted in the comments for this answer, if you're allocating the array with new (dynamic allocation) remember to delete your array with:
正如此答案的评论中所述,如果您使用新的(动态分配)分配数组,请记住使用以下命令删除您的数组:
delete[] array;
回答by John Weldon
Depending on what you want to initialize to you could do any of:
根据您要初始化的内容,您可以执行以下任何操作:
char mystr[] = {'h','i',0};
char * myotherstring = "my other string";
char * mythirdstring = "goodbye";
char * myarr[] = {0};
char * myarr[] = {&mystr, myotherstring};
char * myarr[10];
char * myarr[10] = {0};
char * myarr[10] = {&mystr, myotherstring, mythirdstring, 0};
etc. etc.
等等等等
回答by Don Doerner
One thing I have noticed that you must be careful of... C and C++ have diverged a bit in initialization syntax. As Mark B. points out above, you can initialize an array of char pointers thusly:
我注意到一件事,你必须小心... C 和 C++ 在初始化语法上有点分歧。正如 Mark B. 在上面指出的那样,您可以这样初始化一个字符指针数组:
const char* messages[] =
{
"Beginning",
"Working",
"Finishing",
"Done"
};
But in C++. as kriss points out, this nets you a warning about a deprecated conversion from string to char*. That's because C++ assumes you'll want to use strings for strings ;-}.
但是在 C++ 中。正如 kriss 指出的那样,这会向您发出警告,警告您从字符串到 char* 的转换已被弃用。那是因为 C++ 假设您希望将字符串用于字符串 ;-}。
That's not always true. So when you really do want to initialize an array of char*, I have found that I have to do it like so:
这并不总是正确的。所以当你真的想要初始化一个 char* 数组时,我发现我必须这样做:
const char* messages[] =
{
(char*)("Beginning"),
(char*)("Working"),
(char*)("Finishing"),
(char*)("Done")
};
The compiler is now happy...
编译器现在很高兴......
回答by Max Shawabkeh
Like this:
像这样:
char* my_c_string;
char* x[] = { "hello", "world", 0, my_c_string };
回答by kriss
Like that:
像那样:
char p1 = 'A';
char p2 = 'B';
char * t[] = {&p1, &p2};
std::cout << "p1=" << *t[0] << ", p2=" << *t[1] << std::endl;
But somehow I believe that's not the answer to the real question...
但不知何故,我相信这不是真正问题的答案......
I you want an array of C strings defined at compile time you should use an array of const char * instead:
如果你想要一个在编译时定义的 C 字符串数组,你应该使用一个 const char * 数组来代替:
const char * t2[] = {"string1", "string2"};
std::cout << "p1=" << t2[0] << ", p2=" << t2[1] << std::endl;
Without the const my compiler would say : warning: deprecated conversion from string constant to ‘char*'
如果没有 const,我的编译器会说:警告:不推荐使用从字符串常量到 'char*' 的转换
回答by Mark B
If you really just want a C-style array of constant strings (for example indexed messages):
如果你真的只想要一个 C 风格的常量字符串数组(例如索引消息):
const char* messages[] =
{
"Beginning",
"Working",
"Finishing",
"Done"
};
If however you're trying to maintain a container of runtime variable strings, using the C++ facility std::vector<std::string>
will make keeping track of all the memory operations much easier.
但是,如果您尝试维护运行时变量字符串的容器,则使用 C++ 工具std::vector<std::string>
将使跟踪所有内存操作变得更加容易。
std::vector<std::string> strings;
std::string my_string("Hello, world.")
strings.push_back("String1");
strings.push_back(my_string);
回答by xian
#include <iostream>
int main(int argc, char *argv[])
{
char **strings = new char *[2]; // create an array of two character pointers
strings[0] = "hello"; // set the first pointer in the array to "hello"
strings[1] = "world"; // set the second pointer in the array to "world"
// loop through the array and print whatever it points to out with a space
// after it
for (int i = 0; i < 2; ++i) {
std::cout << strings[i] << " ";
}
std::cout << std::endl;
return 0;
}
回答by Peter Alexander
Just like any other array:
就像任何其他数组一样:
char *a, *b, *c;
char* cs[] = {a, b, c}; // initialized
cs[0] = b; // assignment