# Passing Arrays into Functions

• 05-03-2007, 10:24 PM
Catalyst8487
Passing Arrays into Functions
my issue is probably something very easy, but i've been banging my head all day at this, among other coding issues, and no luck.

I am trying to program a chess game in C++. Currently I am simply working on printing the board. I have 2 arrays, named whiteArray and blackArray that each have 16 elements, assigned based on the pieces positions on the board. At the beginning of a game the arrays look like:

int board[64] = {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};

int blackArray[16] = {1, 2, 3, 4, 5, 6, 7, 8,
9, 10, 11, 12, 13, 14, 15, 16};
int whiteArray[16] = {49, 50, 51, 52, 53, 54, 55, 56,
57, 58, 59, 60, 61, 62, 63, 64};

As the pieces move, I will update the white and black arrays accordingly to reflect where specific pieces are. I want to pass the white and black arrays into a function where it checks the values against the board to find a match.

So this long winded version breaks down to.... how do I pass white and black array into a function?
• 05-03-2007, 11:38 PM
nspils
bool checkMove( blackArray ) ... gives a reference to the first element of the array
• 05-04-2007, 01:09 AM
Danny
You can't pass the entire array to a function. Instead, you pass the array's name, which is actually the address of the first element. You then use pointer arithmetic to traverse the array in the function:
bool checkMove( int * arr);//function definition
{
if (arr[0] ==1)
return true;
return false;
}

checkMove( blackArray ); //call that function
• 05-07-2007, 04:32 PM
Catalyst8487
Ugh, pointers. I hate those things.... they make me :confused:

I tried a couple different things, couldn't get it how I need/want it. Here is the code:
Code:

```void CreateBoard(int *array) {         int boardPos = 1;         int rowCount = 1;         cout << "            |---------------------------------------| \n";         do         {                 cout << "            |    |    |    |    |    |    |    |    | \n";                 for (int arrayCount = 0; arrayCount <= 15 ; arrayCount++)                 {                         for (int row = 1 * rowCount; row <= 8 * rowCount; row++)                         {                                 if (array[arrayCount] == row)                                 {                                         switch (arrayCount)                                         {                                                 case 1, 2, 3, 4, 5, 6, 7, 8:                                                         cout << "|wP  ";                                                         break;                                                 case 9:                                                         cout << "|wR  ";                                                         break;                                                 case 10:                                                         cout << "|wKn ";                                                         break;                                                 case 11:                                                         cout << "|wB  ";                                                         break;                                                 case 12:                                                         cout << "|wQu ";                                                         break;                                                 case 13:                                                         cout << "|wK  ";                                                         break;                                                 case 14:                                                         cout << "|wB  ";                                                         break;                                                 case 15:                                                         cout << "|wKn ";                                                         break;                                                 case 16:                                                         cout << "|wR  ";                                                         break;                                                 }                                 }                         }                 }                 cout << "            |    |    |    |    |    |    |    |    | \n";                 cout << "            |---------------------------------------| \n";                 rowCount++;         }         while (rowCount <= 8);         return; }```
As I am drawing the board I want to print what piece is there. Hence the need to check the white and black arrays and their positions. The first 8 elements are the pawns, followed by rook, knight, bishop, queen, king, bishop, knight, rook for the back 8 elements of the array.

I'm pretty certain my error lies in how I am trying to view the array being passed in, but I've pulled out my books with no luck and now I'm just banging my head against the wall again.

Any help is appriciated!

Chris
• 05-07-2007, 10:21 PM
nspils
have you tried to do this with your formal parameter being int [] array rather than int* array? I don't think you can switch from passing a pointer to then using the indexing [] method of moving from element to element. If you are going to use pointers, try using pointer arithmetic to move to the start of the next element. for this, it would be [array + ( arrayCount * 4 ) ] to get to the element of interest.
• 05-07-2007, 11:47 PM
jonnin
actually you can mix and match notation. I always use [] notation personally, I find the *var notation to be cryptic (I have a lot of math code and *x * *y is a bit sloppy).

I also generally make the function parameters with the * as discussed, not really any good reason for it but I do not care for empty [] pairs.
• 05-08-2007, 03:40 AM
Danny
Quote:

Originally Posted by nspils
have you tried to do this with your formal parameter being int [] array rather than int* array? I don't think you can switch from passing a pointer to then using the indexing [] method of moving from element to element. If you are going to use pointers, try using pointer arithmetic to move to the start of the next element. for this, it would be [array + ( arrayCount * 4 ) ] to get to the element of interest.

Actually, there's no problem mixing the two. In fact, passing arr[] as a function parameter is identical to passing int* because arrays decay into pointers anyway.
The problem with the code is that the array's size isn't passed as a second parameter to the function, the the function can't tell where the array ends:

void func(int * arr, int arrsize);
• 05-08-2007, 07:48 AM
drkybelk
Hi,
why don't you avoid pointers altogether by using std::vector??
Code:

```std::vector<unsigned char> board; std::vector<unsigned char> black; std::vector<unsigned char> white; // fill the board with numbers 1 to 64 for(unsigned char num=1; num < 65; ++num)     num.push_back(num); // analogous for black and white //... void makeMove(vector<unsigned char>& position, unsigned char from, unsigned char to) {     size_t theSize = position.size();     // do a size check here     if(to >= theSize || from >= theSize)     {         cout << "Error" << endl;     }     else     {         position[to] = from;         position[from] = 0;     } } // call like this: makeMove(black, 5, 56);```
saves you the trouble with pointers and is almost as efficient.

Cheers,
D