Reputation: 57
I am trying to make a tic tac toe game and I want to pass my 2d array of the tic tac toe board positions to my function that draws the updated board. I want the parameter value of my function "updateBoard" to be able to take in the memory adress of the board from main so I can use it all throughout my program without worrying about scope. I am getting an error as so when i compile it:
error C2664: 'updateBoard' : cannot convert parameter 1 from 'char (*)[3][3]' to 'char *[][3]' 1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
heres my code:
#include "stdafx.h"
#include <iostream>
using namespace std;
void updateBoard (char *n[3][3])
{
}
int getMove ()
{
int input;
cout <<"\n";
cout <<"1 for square 1| 2 for square 2| and so on... : ";
cin >>input;
return 0;
}
int main ()
{
const int WIDTH = 3;
const int HEIGHT = 3;
char board [WIDTH][HEIGHT] = {' ', ' ', ' ',
' ', ' ', ' ',
' ', ' ', ' '};
updateBoard(&board);
char f;
cin >>f;
}
Upvotes: 0
Views: 190
Reputation: 310960
You can do the following way
#include "stdafx.h"
#include <iostream>
using namespace std;
const int WIDTH = 3;
const int HEIGHT = 3;
typedef char TBoard [WIDTH][HEIGHT];
void updateBoard ( TBoard board, int width )
{
}
int main ()
{
TBoard board = {' ', ' ', ' ',
' ', ' ', ' ',
' ', ' ', ' '};
updateBoard( board, WIDTH);
char f;
cin >>f;
}
As for your error then the function parameter shall be defined as
void updateBoard (char ( *n )[3][3])
{
}
char ( *n )[3][3]
means pointer to a two-dimensional array while
char * n[3][3]
means a two dimensional array of pointers char *
And inside the function you should to write
( *n )[i][j]
to access the element with indexes i and j.
Upvotes: 1