Thread: passing array to function

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    1

    passing array to function

    how do i pass an array of class objects to a function? this is what my code looks like so far..
    Code:
    int play(Cell **cellArray, int size)
    {
        if(!cellArray)
        {
              cerr << "Board contains no live cells." << endl;
              exit(1);
        }
        
        Cell temp[10][10]; //temp array so the final values will not be altered
        
        for(int i=0; i<10; i++)
         {
            for(int j=0; j<10; j++)
            {
                temp[i][j] = cellArray[i][j].getValue();   
            }
         }
        
        int legalTurn = 0;
        int fitness = 0;
        int lastTurn = 0;
        bool anyLife = false;
      
        //for loop that goes through the array and sends the values to the
        //check function to determine if the cell should be alive or dead
        for(int x=0; x<100; x++)
        {
                for(int y=0; y<10; y++)
                {
                        for(int z=0; z<10; z++)
                        {
                                int living = check(c, y, z);
                                if(living > 0)
                                {
                                          temp[y][z] = 1;
                                          anyLife = true;
                                          if(x==100-1)
                                          {
                                                      lastTurn++;
                                          }
                                }
                                else 
                                     temp[y][z] = 0;
                        }
                }
                
                if(anyLife)
                {
                           legalTurn++;
                }
                
                anyLife = false;
                
                for(int i=0; i<10; i++)
                {
                        for(int j=0; j<10; j++)
                        {      
                               cellArray[i][j] = temp[i][j].getValue();
                        }
                }
        }
        
        fitness = legalTurn * lastTurn;
        return fitness;
    }
    i was told that the function header was correct but i cannot figure out how to pass in an array of objects to this method.. i was thinking .. int fitness = play(cellArray, 100); -- but that didnt work.. any ideas?

  2. #2
    Registered User Kurisu's Avatar
    Join Date
    Feb 2006
    Posts
    62
    does something like below doesn't work for yah? (i can't 100% remember how to pass in arrays off the top of my head.. quite sad really.. for some reason my brain thinks the following will work)
    Code:
    int play(Cell cellArray[][], int size);
    Code:
    int fitness = play(cellArray, 100);

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Stick to array notation with multi dimensional arrays. The left most dimension is not specified, so you would do something like this:
    Code:
    int play(Cell cellArray[][10], int rows)
    {
    }
    
    Cell cellArray[8][10] = {0};
    int fitness = play(cellArray, 8)
    If you want to use pointer notation, you would have to do this:
    Code:
    int play( Cell (*cellArray)[10], int rows)
    Last edited by 7stud; 02-20-2006 at 11:09 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Replies: 7
    Last Post: 11-21-2008, 04:27 PM
  3. function passing argument..array ?
    By jochen in forum C Programming
    Replies: 2
    Last Post: 09-30-2007, 11:53 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM