# Thread: pointer to a 2d-array

1. ## pointer to a 2d-array

I don't know the syntax..... a pointer to a 2-dimensional array of chars.

2. Thanks. But now, after seeing that, I realize it's not what I need. I need to be able to define a 2-d array prior to knowing its size, and then size it later. I thought a pointer would do the trick because it does with 1-d arrays. So- what should I use?

3. Why do you need a 2D array?

The best method is not to use pointers for any dynamic arrays if you can help it; a std::vector of std::vectors of ints can work as a 2D array, and it's easier to setup than the pointer to pointer method, as well as being a far better choice in most cases.

Something like this, perhaps:

Code:
```#include <vector>

int rowCount = 5, colCount = 4, initialVal = 9;

typedef std::vector<int> row;
typedef std::vector<row> array2D;

array2D myArray(rowCount, row(colCount, initialVal));
// This creates an array with rowCount rows, each of which is of width colCount.
// Every element starts with value initialVal.```
This would create a 5x4 array of integers. You can access them by myArray[0][0] through myArray[4][3]. All elements will be initialized to 9 originally (you can omit all references to initialVal if you want the array to be initialized to all zeros).

It might not be acceptable, depending on your application, as the array is not contiguous in memory. Each row is contiguous -- e.g. myArray[0][1] is immediately after myArray[0][0] in memory, but myArray[1][0] is not necessarily anywhere near myArray[0][3] in memory. If you need an array that is contiguous in memory, it takes a more complicated approach.

If at all possible, consider not using pointers for dynamic arrays; std::vector offers comparable access time and it's much safer. Notice we never make any calls to new or delete, and we never need to.

Edit: for an array of chars, just replace std::vector<int> with std::vector<char>, and change the type and value of initialVal accordingly (it will default to '\0' unless you specify otherwise).

Short demo program:

Code:
```#include <vector>
#include <iostream>

int main()
{

const int rowCount = 5, colCount = 4;
const char initialVal = ' ';

typedef std::vector<char> row;
typedef std::vector<row> array2D;

array2D myArray(rowCount, row(colCount,initialVal));

myArray[0][0] = 'C';
myArray[1][1] = 'a';
myArray[2][2] = 't';

for (int i = 0; i < rowCount; ++i){
for (int j = 0; j < colCount; ++j){
std::cout << "[" << myArray[i][j] << "]";
}
std::cout << std::endl;
}

}```
Output:

Code:
```[C][ ][ ][ ]
[ ][a][ ][ ]
[ ][ ][t][ ]
[ ][ ][ ][ ]
[ ][ ][ ][ ]```
BTW, one neat feature: given an array, you can find its dimentions. myArray.size() tells the # of rows, myArray[i].size() is the # of cols of row i. Thus, you could print an arbitray array like this:

Code:
```	for (unsigned int i = 0; i < myArray.size(); ++i){
for (unsigned int j = 0; j < myArray[i].size(); ++j){
std::cout << "[" << myArray[i][j] << "]";
}
std::cout << std::endl;
}```
This can be useful when passing arrays to functions. Also, for debugging, you can replace myArray[r][c] with myArray.at(r).at(c) and it will automagically do bounds-checking to make sure that element is within the limits of the array. It is slower, though, so be careful using it.