Thread: arrays and pointers

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    87

    arrays and pointers

    i have this code
    Code:
    #include <iostream>
    
    using namespace std;
    
    int solve_array(int * array[][], int array_x, int array_y)
    {
        int solutions = 1;
        if((array_x == 1) && (array_y==1))
            return 1;
        int array_1[array_x-1][array_y];
        int array_2[array_x][array_y-1];
        solutions = solve_array(array_1[][],array_x-1,array_y) + solve_array(array_2[][],array_x,array_y-1);
        return solutions;
    }
    
    
    
    int main()
    {
        // problem, find number of routes.
        // solution iterate through each possibility
        int array[21][21];
        cout << "the number of possible paths is: " << solve_array(array[][],21,21);
        return 0;
    }
    i need to know how to use an array as an argument and not need a specific size, as it is recursive

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    You could try flattening the 2d array.

    int answer = solve_array (&array[0][0], nrows, ncols);

    This should make the first argument a pointer to the first element. Then in solve_array, you have to do the math array[ x * nrows + y ] for any array[x][y].

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    87
    that works, but i would still like to know how to pass a multidimensional array

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Well a multidimensional array requires sizes for all the dimensions, except for the first, which you didn't seem to want, because of the way you phrased your request.

    If you are familiar with regular array syntax:

    int solve_array (int array[]);

    All you have to do is add another subscript with the size of the dimension inside. You can do this for n dimensions.

    int solve_array (int array[][21], int array_x, int array_y);

    The first argument can be the array name.

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    87
    thanks.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    In C++, a std::vector<std::vector<int *> > can be used to simulate a 2D array of pointers to int. std::vectors keep track of their size.

    In C, there are alternatives, but they require a lot of bookkeeping, so it is easy to get things wrong.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    you could do something like this:
    Code:
    template<int Columns, int Rows>
    int solve_array(int (&array)[Columns][Rows], int array_x, int array_y);
    this way, you can pass in a statically sized array implicitly, and even dynamic arrays by specifying the number of x and y elements as template parameters.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointers to pointers with arrays
    By Nurumla in forum C Programming
    Replies: 3
    Last Post: 07-18-2011, 11:53 PM
  2. Replies: 7
    Last Post: 05-19-2010, 02:12 AM
  3. arrays and pointers
    By beginner1 in forum C Programming
    Replies: 22
    Last Post: 05-23-2009, 11:27 AM
  4. Pointers to 2D arrays
    By bertazoid in forum C Programming
    Replies: 16
    Last Post: 02-26-2009, 04:46 PM
  5. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 05:31 AM