Hi All,

I am extremely new to C++ and would greatly appreciate any advice regarding the following question:

I am trying to create a 2D array of CStrings, however it needs to be declared dynamically for my application. I have read up on numerous forums but I just cant seem to find what I am looking for.

From what I have read the best way to do this would be to use a vector of CStrings. However, I am not to sure how to declare this.

What I would like to be able to do is adress any CString in the following way:

myarray[0][0] = "hello";

Each example I have found requires me to first set the size of the array in the declaration before compilation.

Please could someone point me in the right direction?

What you are asking for can be a very useful data structure... if created dynamically can have about the same performance as a vector and might even be more efficient (a vector might allocate more memory than what you actually need based on the number of push_back() opearations) In either case, here is how you can do both:

The creation of a 2d array is like having a 'pointer to a bunch of pointers':

//create the array pointer (holds the address to the beginning of a block of allocated memory)

//create 2d array of pointers to cstrings
char* words = new char*[wordcount];

//allocate memory for each cstring pointer
for(int i=0; i<wordcount; i++)
{
     wordcount[i] = new char[20];
}
//Now you have words[word#][individual char] array of cstrings, each can hold a word of (in this case 19 chars or less)

//Now you can perform cool cstring operations:
//'Add' to an existing cstring (in this case, the second cstring of the array)
strcat(words[2], " a good day");

If you want to use more modern c++ techniques as opposed to using the older C style character arrays, here is how ye' would declare a 'vector of char vectors'

//Notice that this is one of the very few instances in c++ where spacing matters
//Be sure to add a space in between the > > or else your compiler will think it's a >> extraction operator
vector<char*> words;

//create a temporary cstring we will push into the vector anytime we want to add to it
char* temp = new char[20];

//Once populated, push it back into our vector of cstrings
for(int i=0; i<wordcount; i++)
{
     cout << "Enter a word (19 chars or less): ";
     cin >> temp;

     words.push_back(temp);
}

//Access a cstring from the char* vector:
cout << "Enter element number to display: ";
cin >> element;
cout << words[element];

Edited 7 Years Ago by Clinton Portis: do the stanky leg.

@Clinton Portis:
I suppose, at line #3, you wanted to write:

vector<vector<char*> > words; // this is vector of char* vectors

if so, this won't work:

words.push_back(temp);

Can be done like this:

vector<char*> v_temp;
for(int i=0; i<wordcount; i++)
{
     cout << "Enter a word (19 chars or less): ";
     cin >> temp;

     v_temp.push_back(temp);
}
words.push_back(v_temp);

Hi Clinton,

Thank you very much for your detailed reply...You have definitely helped me clarify a few things.

However I still have a question or two:

In your examples(for which i am extremely grateful) you have showed me exactly how to create the array as well as add to and read a specific element.

Firstly, am i correct in saying that "MyArray" in the following example is two dimensional:

MyArray[0][1]="hello"

If so how would I access an array using your example above in the same "row", "collum" manner?

For instance the following code was pasted on one of the other forums:

#include <vector>
std::vector<std::vector<CString> > your2darray(width,
std::vector<CString>(height));
your2darray[x][y] = "asdf";

Memory management is done automatically here.

However according to the poster this is supposed to be dynamic, however "Width" and "Height" must be still be declared?

I was just driving down the road and I was thinking, "Hmm.. i forgot to allocate memory for each char* of the vector... so I think this will work better:

for(int i=0; i<wordcount; i++)
{
     char* temp = new char[20];
     cout << "Enter a word (19 chars or less): ";
     cin >> temp;

     words.push_back(temp);
}

Pecet, I was first considering a 'vector of a vector of char pointers' I soon realized that was overkill.. a simple vector of char* is similar to a char[][] 2d array.

ghering,

MyArray[0][1]="hello"

will give you the 'e' in "hello".. if you wanted to access the entire word, just dereferrence the first dimension, which is a pointer to the beginning of the cstring

cout << MyArray[0];

As long as the 2nd dimension is null terminated, your cout operation will not run out of bounds into the rest of the array.

Edited 7 Years Ago by Clinton Portis: n/a

Thanks again Clinton,

That is now clear to me.

Just for interest sakes, am I missing something with the following code or do you agree that the "width" and "height" must first be defined?

#include <vector>
std::vector<std::vector<CString> > your2darray(width,
std::vector<CString>(height));
your2darray[x][y] = "asdf";

Thanks again for the help!

I think you mean to do this, if your2darray is an array of cstrings:

//this
your2darray[x][y] = "asdf";

//should be this:
strcpy(your2darray[x], "asdf");
This question has already been answered. Start a new discussion instead.