Create an array of char*(string in C++)

Hmmm... So much for my struggle that even after having learned dynamic memory allocation it took me a couple of hours to figure this out. This may not be interesting  for developers using .net or Java but atleast for students learning C/C++, it is. So how do you create an Array of char* or C strings? Here's a code snippet:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
char** stringArray;

int maxWords = 3;

stringArray = new char*[maxWords];

stringArray[0] = "I";
stringArray[1] = " Love ";
stringArray[2] = "C++";



for (int i = 0; i < maxWords; i++)
{
std::cout << stringArrayIdea;
}
 

Now I'm no expert C programmer to explain all that stuff. It's pretty straightforward anyway. From the C++ programmer point of view, C strings should be avoided because they bring about adisaster in your program that is hard to debug. Still, they're fun to use in small-scale applications and it's mandatory that every C/C++ programmer learn how to use them.
Published 03-05-2007 12:27 AM by lamia
Filed under: