arrays and pointers

This is a discussion on arrays and pointers within the C++ Programming forums, part of the General Programming Boards category; i have this code Code: #include <iostream> using namespace std; int solve_array(int * array[][], int array_x, int array_y) { int ...

  1. #1
    Registered User bobknows's Avatar
    Join Date
    Jan 2011
    Location
    SD
    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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,752
    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 bobknows's Avatar
    Join Date
    Jan 2011
    Location
    SD
    Posts
    87
    that works, but i would still like to know how to pass a multidimensional array

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,752
    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 bobknows's Avatar
    Join Date
    Jan 2011
    Location
    SD
    Posts
    87
    thanks.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,531
    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 in reply to you, it is likely you deserve it. Suck it up, sunshine, and read this, this, and this before posting again.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    2,545
    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-19-2011, 12:53 AM
  2. Replies: 7
    Last Post: 05-19-2010, 03:12 AM
  3. arrays and pointers
    By beginner1 in forum C Programming
    Replies: 22
    Last Post: 05-23-2009, 12:27 PM
  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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21