two-dimensional array of pointers

So, I use the following code to create a two dimensional array of pointers with a specified height and width and then to read values from a file.

int** firstGrid;
firstGrid = new int*[width];

for (int i = 0; i < width; i++)
{
firstGrid[i] = new int [height];
}

//Populated by:
for ( int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
in >> nextNumber;
firstGrid[i][j] = nextNumber;
}
}

All seems to work great and then I'm trying to print it out.
I've tried:
cout << firstGrid[i][j];
//Which gives the output of the address.
cout << *firstGrid[i][j];
//Which simply won't compile.
cout << *&firstGrid[i][j];

Any ideas? Thanks all
You don't have a 2-d array of points. You have a 2-d array of ints, or a 1-d array of int*. A 2-d array of points would look like int*[][] or int***.
Okay, well any idea how I would print the value of what I do have?
The first option. firstGrid[i][j] will print the integers you allocated in the 2-d array. In this code, though, you haven't initialized them so expect them to be random garbage.
Last edited on

cout << *&firstGrid[i][j];


where you get this idea? i say, it won't compile, of course
Topic archived. No new replies allowed.