初始化一个 Char*[]

发布于 2024-08-21 20:10:57 字数 45 浏览 7 评论 0原文

标题中的问题是,如何在 C++ 中初始化 char*[] 并为其赋值,谢谢。

Question is in the title, how do I initialize a char*[] and give values to it in C++, thank you.

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

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

发布评论

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

评论(8

贱人配狗天长地久 2024-08-28 20:10:57

尽管您可能知道, char*[] 是一个指向字符的指针数组,我猜您想存储多个字符串。初始化此类指针的数组非常简单:

char ** array = new char *[SIZE];

...或者如果您在堆栈上分配内存:

char * array[SIZE];

那么您可能希望用循环填充该数组,例如:

for(unsigned int i = 0; i < SIZE; i++){
    // str is likely to be an array of characters
    array[i] = str;
}

正如此答案的注释中所述,如果您正在使用新的(动态分配)分配数组,请记住使用以下命令删除数组:

delete[] array;

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 ** 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;
眼睛会笑 2024-08-28 20:10:57

根据您想要初始化的内容,您可以执行以下任一操作:

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

等等。

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.

深海夜未眠 2024-08-28 20:10:57

我注意到你必须小心的一件事...C 和 C++ 在初始化语法上有些不同。正如 Mark B. 上面指出的,您可以这样初始化一个 char 指针数组:

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

但是在 C++ 中。正如 kriss 指出的那样,这会向您发出有关从 string 到 char* 的已弃用转换的警告。这是因为 C++ 假设您希望使用字符串来表示字符串 ;-}。

这并不总是正确的。因此,当您确实想要初始化 char* 数组时,我发现我必须这样做:

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

编译器现在很高兴......

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:

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 ;-}.

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:

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

The compiler is now happy...

吾性傲以野 2024-08-28 20:10:57

像这样:

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

Like this:

char* my_c_string;
char* x[] = { "hello", "world", 0, my_c_string };
羁绊已千年 2024-08-28 20:10:57

如果您确实只想要一个 C 风格的常量字符串数组(例如索引消息):

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

但是,如果您尝试维护运行时变量字符串的容器,请使用 C++ 工具 std::vector 将使跟踪所有内存操作变得更加容易。

std::vector<std::string> strings;
std::string my_string("Hello, world.")
strings.push_back("String1");
strings.push_back(my_string);

If you really just want a C-style array of constant strings (for example indexed messages):

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.

std::vector<std::string> strings;
std::string my_string("Hello, world.")
strings.push_back("String1");
strings.push_back(my_string);
呢古 2024-08-28 20:10:57

就像这样:

char p1 = 'A';
char p2 = 'B';
char * t[] = {&p1, &p2};

std::cout << "p1=" << *t[0] << ", p2=" << *t[1] << std::endl;

但不知怎的,我相信这不是真正问题的答案...

我想要一个在编译时定义的 C 字符串数组,你应该使用 const char * 数组代替:

const char * t2[] = {"string1", "string2"};

std::cout << "p1=" << t2[0] << ", p2=" << t2[1] << std::endl;

如果没有 const 我的编译器会说:
警告:不推荐从字符串常量到“char*”的转换

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:

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

書生途 2024-08-28 20:10:57

就像任何其他数组一样:

char *a, *b, *c;
char* cs[] = {a, b, c}; // initialized
cs[0] = b; // assignment

Just like any other array:

char *a, *b, *c;
char* cs[] = {a, b, c}; // initialized
cs[0] = b; // assignment
Oo萌小芽oO 2024-08-28 20:10:57
#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;
}
#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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文