Passing a 1 dimensional array

Below is not my complete program code. The issue I wanted to focus on is when I use [g.playGiven("game 0", game0, dimension);] it doesn't appear to be passing the whole game0 array, but only the first element. I can't figure out why this is the case and I have been fighting it for a while now.

The reason I don't believe it is passing the whole array is when I debug the program and go to my playGame() and look at boardArray[] variable I find it only has one element (or one value, 1). I can't figure out why this is the case and I was hoping to get some help.

Thanks!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//Main.cpp
int main()
{
	const int SIZE = 3;
	const int dimension = SIZE * SIZE;
	Game g;
	int game0[SIZE*SIZE] = { 1, 2, 3, 7, 4, 0, 6, 5, 8 };
	g.playGiven("game 0", game0, dimension); //game0 is not passing whole array
}

//inside Game.cpp
void Game::playGiven(std::string boardName, int boardArray[], int size) {
	inital.makeBoard(boardArray, size);
	solve();
}

//inside Board.cpp
//Create a board from a given set of values
void Board::makeBoard(int values[], int size) {
	int c = 0;
	for (int i = 0; i < SIZE; i++)
		for (int j = 0; j < SIZE; j++) {
			if (values[c] == 0) {
				blankRow = i;
				blankCol = j;
			}
			board[i][j] = values[c++];
		}
}

//inside Game.h
//defines the game class
class Game {
private:
	Queue g;
	Board inital;
	Board final_1;
public:
	void playGiven(std::string, int array[], int); //Use a userdefined board
	void playRandom(std::string, const int); //From a perfect board, use the random function within board to generate a random board.			void solve(); // Solve the puzzle
};

//Board.h
//defines the board class
class Board {
private:
	char lastMove;
	string boardMoves;
public:
	void setMove(char);
	void appendBoardMoves(char);
	char getMove();
	string getBoardMoves();

	static const int SIZE = 3;
	int board[SIZE][SIZE];  // Values of board
	Board(const Board & b);  //Create board from another board
	void makeBoard(int jumbleCt = 0);  //Starting from a perfect board, do jumbleCt moves to alter it
	void makeBoard(int values[], int); // Create a board from a set of values specified in row major order
	string toString() const;  //return a string which represents the contents of the boar
	bool operator==(Board &b);   //Return true if two boards are equal
	int blankRow;   // Row location of blank
	int blankCol;   // Column locatio of blank
	Board() { makeBoard(); };
	bool slideUp();  // If possible, slides a tile up into the blank position.  Returns success of operation.
	bool slideDown(); // If possible, slides a tile down into the blank position.  Returns success of operation.
	bool slideLeft(); // If possible, slides a tile left into the blank position.  Returns success of operation.
	bool slideRight(); // If possible, slides a tile right into the blank position.  Returns success of operation.
	void jumble(int ct);  //Do jumble moves to alter boardf
	char makeMove(char m, char lastMove);  //Makes move indicated by m, returns character name of move if done, will not undo previous lastMove
};
Hi Friend,

in c++ the name of an array is the the pointer to the first element of the array.
you can access the elements of the array in a for loop:

int n;

for(int i = 0; i < size_of_array; i++){
n = game0[i];
}
The reason I don't believe it is passing the whole array is when I debug the program and go to my playGame() and look at boardArray[] variable I find it only has one element (or one value, 1).
The reason for this behavior is that the array is implicitly converted to a pointer when using the parameter like that. Therefore you also pass the size parameter.

You can pass an array as an array like so:
1
2
// Pass arrays with 3 elements only
void Game::playGiven(std::string boardName, int (&boardArray)[3]) {
Thanks for the help! Problem solved!
Topic archived. No new replies allowed.