简体   繁体   English

初始化字符* []

[英]Initializing a Char*[]

问题在标题中,如何初始化char * []并在C ++中给它赋值,谢谢。

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. 虽然你可能知道,char * []是一个指向字符的指针数组,我猜你想要存储一些字符串。 Initializing an array of such pointers is as simple as: 初始化这样的指针数组很简单:

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: 如本答案的评论中所述,如果您使用new(动态分配)分配数组,请记住删除您的数组:

delete[] array;

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. 等等

One thing I have noticed that you must be careful of... C and C++ have diverged a bit in initialization syntax. 有一点我注意到你必须小心...... C和C ++在初始化语法方面有所分歧。 As Mark B. points out above, you can initialize an array of char pointers thusly: 正如Mark B.上面指出的那样,你可以初始化一个char指针数组:

const char* messages[] =
{
    "Beginning",
    "Working",
    "Finishing",
    "Done"
};

But in C++. 但在C ++中。 as kriss points out, this nets you a warning about a deprecated conversion from string to char*. 正如kriss指出的那样,这会让你发出一个关于从字符串到char *的弃用转换的警告。 That's because C++ assumes you'll want to use strings for strings ;-}. 那是因为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... 编译器现在很开心......

Like this: 像这样:

char* my_c_string;
char* x[] = { "hello", "world", 0, my_c_string };

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

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

Just like any other array: 就像任何其他数组一样:

char *a, *b, *c;
char* cs[] = {a, b, c}; // initialized
cs[0] = b; // assignment
#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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM