Understanding arrays as pointers

Hey, so in class, we learned about understanding arrays as pointers and we used it to print out a chessboard and now I'm trying to do the same on my own and I'm printing out a reverse 'L' but for some reason, it's printing out the location of a1 rather than what a1 is. I'm new to this so please go easy on me. Thank you

Here is the code:
***

void Tictactoe()
{
typedef char box[4][6];
box a1, a2, a3, x1, x2, x3, *board[3][3];
a1[0][5] = char(219);
a1[1][5] = char(219);
a1[2][5] = char(219);
a1[3][5] = char(219);
a1[3][5] = char(219); a1[3][4] = char(219); a1[3][3] = char(219); a1[3][2] = char(219);
a1[3][1] = char(219); a1[3][0] = char(219);
board[0][0] = &a1;
cout << *(board[0][0]) << endl;;

}
int main(){

Tictactoe();




return 0;
}

***
Last edited on
1
2
typedef char Box[4][6];
Box* board[3][3];

A board is a 2D array that has nine (3x3) elements.
Each element is a pointer to a Box.
A Box is a 2D array that has 24 char elements.

The board[0][0] is a pointer to a char[4][6] 2D array.

*(board[0][0]) == board[0][0][0] == a1[0]

You do cout << a1[0].
Last edited on
Thank you appreciate it.
Isn't it:

*(board[0][0]) == board[0][0][0] == a1

Dereferencing board[0][0], a Box*, yields a Box, which is what a1 is.
Last edited on
What I was trying to do in the example above was store the location of a1 into box[0][0] because let's say i have a variable called x and x=2 and if x = 2 I change that box[0][0]'s value from a1 to x1 and instead print out x1.
@dutch: Thanks. The arrays of arrays distract.

@john: Why so complicated? Why can't you start with one pointer?
1
2
3
4
5
6
7
8
9
10
void print( box* b );

box a1;
box x1;
if ( x == 2 ) {
  print( x1 );
}
else {
  print( a1 );
}

and
1
2
3
4
5
6
7
8
void print( box* b ) {
  for ( int row=0; row < 4; ++row ) {
    for ( int col=0; col < 6; ++col ) {
      std::cout << ' ' << b[row][col];
    }
    std::cout << '\n'
  }
}
You're right. Thank you, my good sire,
Topic archived. No new replies allowed.