Skip to main content
gamedev.net
🔒 Locked

Problems with Array

Started by Hammonjj Jun 17, 2007 at 10:16 PM 5 replies 700+ views
Original Post
Hammonjj
Hammonjj
I'm trying to initialize and array of char to ' ', but I'm having some issues.

void initailize_board(char ** board);



Is my declaration

char gameBoard[3][3];
initailize_board(gameBoard);



Is my use of the function in my program

void initailize_board(char ** board) {
	// Initialize entire array to ' '
	for(int index1 = 0; index1 < 3; index1++)
	{
		for(int index2 = 0; index2 < 3; index2++)
		{
			board[index1][index2] = ' ';
		}
	}
}



Is my function definition. I keep getting an error: - error: cannot convert 'char (*)[3]' to 'char**' for argument '1' to 'void initailize_board(char**)' Thanks again for all the help!
James HammondUniversity of ColoradoDepartment of Computer Science
yadango
yadango
change initailize_board(char ** board) to initailize_board(char board[3][3])

btw, u misspelled initialize :-P. you also have to remember that an array is not a pointer (for example: an array cannot be an l-value but a pointer can).
Kuppikeitto
Kuppikeitto
Hi , try
char gameBoard[3][3] = {' '};

/Kuppikeitto
rip-off
rip-off
Quote:
Original post by Kuppikeitto
Hi , try
char gameBoard[3][3] = {' '};

/Kuppikeitto


Nope, that sets the first element to ' ', the rest to 0.

Quote:
Original post by Verg
Quote:
Original post by Hammonjj
I'm trying to initialize and array of char to ' ', but I'm having some issues.


why not just do this?

::memset(gameBoard, 32, (sizeof(char) * 3 * 3)); // '32' is ASCII for " "


Dont use 32, just use ' ', it will compile the same and is far more readable:
::memset(gameBoard, ' ', (sizeof(char) * 3 * 3));
Zahlman
Zahlman
Don't write sizeof(char) (except implicitly via a template that has a sizeof(T)); sizeof(char) == 1 everywhere, always, by definition. Also, in C++, use std::fill instead.

But the main conceptual problem for the OP is that a char[3][3] is actually one-dimensional storage: 9 chars of memory in a row are reserved, and there is nothing put into memory at any time during program execution that indicates the division into 3 "rows" of 3 chars each. All of that interpretation is done at compile time.

If you want a real multi-dimensional array, then use - well, it depends on your requirements. If you really do know the size ahead of time (e.g. for a tic-tac-toe board), then a boost::array of boost::arrays is appropriate:

typedef boost::array<boost::array<char, 3> 3> board_t;board_t board;// If you only need to initialize boards of that size:void initialize_board(board_t& b) {  // access b[j] just as you'd expect to be able to}// If, for any given board, you'll know its size in advance BUT you need to// initialize multiple sizes of boards, you can use templates:template<int x, int y>void initialize_board(boost::array<boost::array<char, x>, y>& b) {  for (int j = 0; j < y; ++j) {    for (int i = 0; i < x; ++i) {      // some complicated calculation goes here    }  }}


You can also use std::fill on each row of a boost::array. (I don't think it will necessarily work over the whole thing because of structure padding.)

If you need the array to be rectangular, but you don't know its size ahead of time, then boost::multi_array is appropriate:

typedef boost::multi_array<char, 2> board;// this time, the int parameter to the template is the dimensionality.void initialize_board(board& b) {  for (int j = 0; j < b.shape()[1]; ++j) {    for (int i = 0; i < b.shape()[0]; ++i) {      // some complicated calculation goes here    }  }}


And you can std::fill the multi_array all in one go to initialize each element to the same value (it provides a .begin() and .end(), so you don't even have to fiddle with array indices to make that work).

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.