Dilemna: cannot convert * to ** or invalid types for array subscript

Started by
4 comments, last by SnotBob 15 years, 11 months ago
Here's the problem. I have two functions. The first takes in a pointer to an array (or so I believe, I'm still bad with pointers and references) and proceeds to the array to a value. The second calls the first function. The first problem I ran into was that I had to pass in a pointer to an array otherwise the array in the function had invalid types. Then I ran into the problem of not being able to convert the array in the first function into a double pointer. To make things clearer, here's the code (the exact wording of the first error might be off)

void clearBoard(COLOR *board[])
{
	
	for(int y = 0; y < BOARD_HEIGHT; y++)
	{
		for(int x = 0; x < BOARD_WIDTH; x++)
		{
			board[x][y] = NONE; //invalid types COLOR[int] for array subscript
		}
	}
}

void setBoard()
{
	clearBoard(*checkBoard); //Cannot convert COLOR* to COLOR**
        // function does other things
}
Advertisement
Actually, your function clearBoard() takes an array of COLOR pointers as an argument. You can't have a pointer to an array, without specifying the array dimension. Multidimensional arrays and array/pointer conversions are the hairier bits of C.

Assuming that this is C, your best bet is probably to use a one dimensional array of size BOARD_WIDTH*BOARD_HEIGHT. Then you can access the element at x,y with board[BOARD_WIDTH*y+x].
Try this.

void clearBoard(COLOR board[]){		for(int y = 0; y < BOARD_HEIGHT; y++)	{		for(int x = 0; x < BOARD_WIDTH; x++)		{			board[x + y*BOARD_WIDTH] = NONE; //invalid types COLOR[int] for array subscript		}	}}void setBoard(){	clearBoard(checkBoard); //Cannot convert COLOR* to COLOR**        // function does other things}


=Loren Tapia=
this line

clearBoard(*checkBoard);


should probably be

clearBoard(checkBoard);


assuming that checkBoard is defined like so

COLOR checkBoard[BOARD_WIDTH][BOARD_HEIGHT];



What

clearBoard(*checkBoard);


means is "dereference the checkBoard pointer" which means "give me whatever checkBoard points to". What you want is checkBoard itself.
This probably should have been mentioned before, but I'm using C++.

Harry Hunt:
If I only pass in checkBoard, it says that it cannot convert COLOR(*)[17] to COLOR**. What worries me is that the array should be 2d. Yet if I make clearBoard(COLOR board[]) then I get the error "invalid types COLOR[int] for array subscript"
Your assumption about checkBoard is correct.

It should also be mentioned that COLOR is an enumeration.

Are the workarounds necessary for passing a 2d array to a function?
Quote:Original post by SomeoneX
If I only pass in checkBoard, it says that it cannot convert COLOR(*)[17] to COLOR**.
Are the workarounds necessary for passing a 2d array to a function?

The error message tells your solution: clearboard(COLOR (*board)[17]), or you could put clearboard(COLOR board[WIDTH][HEIGHT]). The problems with these are that you need to know the board size at compile time. If this is not an issue, then you can use the above. Otherwise you can do something like this:
class Board { private:  std::vector<COLOR> data; public:  Board(int width,int height) :     width(width),height(height),data(std::vector(width*height)  {  }  COLOR & operator()(int x,int y) { return data[x+y*width]; }  COLOR operator()(int x,int y) const { return data[x+y*width]; }}// ... In some function  Board myBoard;  COLOR c=myBoard(3,4);

This topic is closed to new replies.

Advertisement