views:

184

answers:

7

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

+1  A: 

Just like any other array:

char *a, *b, *c;
char* cs[] = {a, b, c}; // initialized
cs[0] = b; // assignment
Peter Alexander
+2  A: 

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.

John Weldon
Romain
I edited my answer slightly, and as it is now it should work.
John Weldon
There is also the issue of the difference between a char * and a const char * My compiler use to raise a warning whenever I try to assign a const char * (constant strings) to a char *.
kriss
ya... this was really intended to demonstrate initialization rather than compiler satisfaction :)
John Weldon
+4  A: 

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;
Stephen Cross
`char ** array = new char[SIZE];` should be `char ** array = new char *[SIZE];`
kitchen
So it should, thanks for pointing it out :)
Stephen Cross
You may also remind that if array is allocated dynamically it should be deleted using `delete [] array` (using `delete array` is a current newbie mistake).
kriss
Yeah, that's a good additional note
Stephen Cross
+2  A: 

Like this:

char* my_c_string;
char* x[] = { "hello", "world", 0, my_c_string };
Max Shawabkeh
A: 
#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;
}
kitchen
+1  A: 

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

kriss
+1  A: 

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