Passing Arrays into Functions


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 8 of 8

Thread: Passing Arrays into Functions

  1. #1
    Join Date
    May 2007
    Posts
    2

    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?

  2. #2
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    bool checkMove( blackArray ) ... gives a reference to the first element of the array

  3. #3
    Join Date
    Nov 2003
    Posts
    4,118
    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
    Danny Kalev

  4. #4
    Join Date
    May 2007
    Posts
    2
    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

  5. #5
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    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.

  6. #6
    Join Date
    Dec 2003
    Posts
    3,366
    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.

  7. #7
    Join Date
    Nov 2003
    Posts
    4,118
    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);
    Danny Kalev

  8. #8
    Join Date
    Jan 2005
    Location
    UK
    Posts
    604
    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
    DKyb
    -------------------------------
    Life is a short warm moment -
    Death is the long cold rest.
    Pink Floyd
    -------------------------------

Similar Threads

  1. arrays and functions
    By ChrisB in forum C++
    Replies: 3
    Last Post: 03-13-2007, 02:58 PM
  2. Passing Arrays in COM
    By Johan Odin in forum C++
    Replies: 0
    Last Post: 03-14-2006, 05:03 AM
  3. Passing arrays to Oracle stored procedure
    By NJMrbig in forum VB Classic
    Replies: 0
    Last Post: 01-16-2002, 09:07 AM
  4. Replies: 1
    Last Post: 08-06-2001, 11:04 AM
  5. Arrays and Functions
    By Matt in forum Java
    Replies: 1
    Last Post: 09-24-2000, 01:05 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center