array of an array

So I want to create a 5x4 2D array like this:

[][][][]
[][][][]
[][][][]
[][][][]
[][][][]

I declare the array like this:

PegType ** peg;
(where PegType is a typedef I define)

How would I initialize the array to create the grid I want? Right now I have the following, but it doesnt work:

1
2
3
4
5
6
7
8
9
	peg = new PegType* [numRows];
	
	// loop through and fill in the array with appropriate values
	for (int i = 0; i < numCols; i++){
		// Fill the pointer array positions with PegType arrays
		peg[i] = new PegType[numRows];	
			
	}
		


NOTE: numRows and numCols hold correct values(I've checked).
Last edited on
If you already know it's going to be 5x4, why not:

PegType[4][5];
Hi

Could you write what it is not working, what you expect your code to do, and what they do ?
Can't you use operator[] or whatever else write it so that we could help you.

Last edited on
I assume it crashes, because you create an array with "numRows" elements, and then access "numCols" elements of it.
+1 hanst99:

You're probably getting array out of bounds errors, because it would appear that you're making a 4x4 grid.
Does it have to be dynamically allocated?
I found out what I was doing wrong, thanks for the responses guys.
Topic archived. No new replies allowed.