Thread: Passing a 2d array of unkown size

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    5

    Passing a 2d array of unkown size

    Code:
    #include <iostream>
    
    using namespace std;
    
    void show(int **array, int size)
    {
        for (int i = 0; i < size; i++)
        {
            for (int j = 0; j < size; j++)
            {
                cout <<array[i][j];
            }
            cout << endl;
        }
    }
    
    int main()
    {
        int array[4][4] =
        {
            { 1, 1, 1, 1 },
            { 1, 0, 0, 1 },
            { 1, 0, 0, 1 },
            { 1, 1, 1, 1 }
        };
    
        show(array, 4);
    
        return 0;
    }
    error: cannot convert `int (*)[4]' to `int**' for argument `1' to `void show(int**, int)'

    I need to pass 2d n by n arrays to a function. However, the sizes will be different but the function will do the same thing. How would I go about doing this?

    Thanks for any help and suggestions
    Last edited by dannyzimbabwe; 05-04-2009 at 11:12 AM.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You could definitely pass a one-dimensional "view" of the array.

    Code:
    #include <iostream>
    
    using namespace std;
    
    void show(int *array, int row_count, int col_count)
    {
        for (int i = 0; i < row_count; i++)
        {
            for (int j = 0; j < col_count; j++)
            {
                cout <<*array;
                ++array;
            }
            cout << endl;
        }
    }
    
    int main()
    {
        int array[4][4] =
        {
            { 1, 1, 1, 1 },
            { 1, 0, 0, 1 },
            { 1, 0, 0, 1 },
            { 1, 1, 1, 1 }
        };
    
        show(array[0], 4, 4);
    
        return 0;
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    147
    You've encountered one of the reasons it's better to use a vector, where size is part of the vector class - or, better still (more flexible) to pass an iterator to the vector.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    In such cases, you'd need to at least be able to specify the last dimension of the array for what you were trying to actually work.

    Code:
    void show(int *array[4], int size)
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    5
    Thank you all for the help. I will use anon's solution until I can learn to design it better.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing multidimensional/dynamic array to function
    By epyfathom in forum C Programming
    Replies: 2
    Last Post: 04-02-2009, 05:39 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. passing 2d array as pointer
    By X PaYnE X in forum C Programming
    Replies: 2
    Last Post: 12-03-2005, 09:28 AM
  4. get nxn size blocks from 2d array?
    By acfror in forum C Programming
    Replies: 4
    Last Post: 10-28-2005, 03:18 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM