Thread: Passing a 2 dimension array to a function

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    1

    Passing a 2 dimension array to a function

    I am just learning C++ programming and want to pass a 2
    dimension array to a function that will oprint this array. My problem is the function needs to know the element size subscript. How can I get this parameter to the function without hardcoding the size in the function?

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Just pass the row/col values to the function:
    Code:
    void print_array(int **array, int row, int col);

  3. #3
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by Monster
    Code:
    void print_array(int **array, int row, int col);
    you don't want to pass a pointer to a pointer to the datatype of the array. You want to pass just a pointer to the datatye of the array. IE

    Code:
    void DoSomething( Datatype* Array, unsigned int NumRows, unsigned int NumCols );
    and then manually calculate element offset from row and column values within the function

    If you happen to know the number of columns before-hand, you can do something like

    Code:
    void DoSomething( Datatype Array[][NumCols], unsigned int NumRows ); // Where NumCols is a constant
    or if you knew both the number of rows and columns before hand you could do

    Code:
    void DoSomething( Datatype (&Array)[NumRows][NumCols] ); // Where NumRows and NumCols are constants

  4. #4
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    I know OOP's method, but I don't know moster's, so how does it work?
    none...

  5. #5
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by ammar
    I know OOP's method, but I don't know moster's, so how does it work?
    It doesn't -- at least without typecasting

  6. #6
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Originally posted by Polymorphic OOP
    It doesn't -- at least without typecasting
    It does work:
    Code:
    #include <iostream>
    
    void print_array(int **array, int row, int col)
    {
       for(int r = 0; r < row; r++)
       {
          for(int c = 0; c < col; c++)
             cout << array[r][c] << " ";
          cout << endl;
       }
    }
    
    int main(void)
    {
       int rows, cols, **array;
    
       cout << "Enter number of rows:";
       cin >> rows;
       cout << "Enter number of columns:";
       cin >> cols;
    
       // allocate memory
       array = new int *[rows];
    
       for(int i = 0; i < rows; i++)
          array[i] = new int[cols];
    
       // fill array
       for(int r = 0; r < rows; r++)
          for(int c = 0; c < cols; c++)
             array[r][c] = r + c;
    
       print_array(array, rows, cols);
    
       // delete memory
       for(int i = 0; i < rows; i++)
          delete [] array[i];
    
       delete array;
    
       return 0;
    }

  7. #7
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by Monster
    It does work
    No, it doesn't. The only reason you got that to work was because you tailored the situation to work with an array of pointers rather than a multidimensional array. They are very different things.

  8. #8
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Originally posted by Polymorphic OOP
    No, it doesn't. The only reason you got that to work was because you tailored the situation to work with an array of pointers rather than a multidimensional array. They are very different things.
    I know they are different things but you can use them the same way (as a multidimensional array).

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    vVv's code is a good example of the debate. vVv declares a clear multidimensional array, foobar, and passes it to foo using a multilevel pointer, p, and uses that multilevel pointer as an array in the function. So, is p a pointer or an array? I say: Who cares! As long as you use it like an array, it's an array. Bottom line: you can achieve the desired result with any of the above techniques. Take your pick.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-21-2008, 04:27 PM
  2. passing an array to function
    By waysgoose in forum C++ Programming
    Replies: 2
    Last Post: 07-22-2008, 03:59 AM
  3. function passing argument..array ?
    By jochen in forum C Programming
    Replies: 2
    Last Post: 09-30-2007, 11:53 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM