开发者

Initializing a Char*[]

Question is in the title, how do I initialize a char*[] and give values to i开发者_开发知识库t in C++, thank you.


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;


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


Like this:

char* my_c_string;
char* x[] = { "hello", "world", 0, my_c_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);


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜