Anonymous
Anonymous asked in Computers & InternetProgramming & Design · 3 weeks ago

How do I have a function return a two dimensional array in c++? Can you show me an example?

4 Answers

Relevance
  • Anonymous
    3 weeks ago

    This place is full of trolls who don't help anyone. 

    C style arrays don't have associated length variables, you can sizeof them but that has issues too (such as in the case of the multidimensional array, like you're referencing).

    and ugg, I hate giving out untested code... but cannot find the specific example I'm looking for, the question being what the primitive for int[][] is (whether it needs 1 * or 2)

    int var[5][6];

    func(var,5,6);

    void func(int **array, int size1, int size2) {

      array[size1-1][size2-1] = 5;

    }

    see things like memcpy for examples of this behavior. You don't need to use classes or anything fancy. It was a simple question, returning an array, and vectors have their own issues.

    If you want the allocation in the function, you have to be aware that getting the length back is rather difficult, so you have pass by references going on.

     

    int var[][];

    int size1;

    int size2;

     

    func(var,size1,size2); 

     

    void func(int &**array, int &size1, int &size2) { 

    array = new int[5][6]();

    size1 = 5;

    size2 = 6;

    }

  • 3 weeks ago

    If you are talking about a built-in array data type, as in:

    int identity[][2] = {{1, 0}, {0, 1}};

    That can't be returned as a function result.   You have to create the array in the calling program and pass it to the function for it to fill in the values.  As in:

    void make_identity(int matrix[2][2])

    {

        matrix[0][0] = matrix[1][1] = 1;

        matrix[0][1] = matrix[1][0] = 1;

    }

    That's the only option in the C language that C++ is derived from.  C++ adds standard template class types std::vector and std::array that can be used to implement array-like objects that can be passed by value and returned as function results.  The std::array<> template was added in C++11 and represents an array of fixed size at compile time, just like the C array type, but with methods matching the other standard library container types.  You describe a 2D array as a 1D array of 1D arrays:

    #include <array> // you'll need this in your includes

    // Make array_2x2 a short alias for a longer std::array type:

    typedef std::array<std::array<int, 2>, 2> array_2x2;

    // A function returning a 2x2 array:

    array_2x2 identity(){    array_2x2 result;    result[0][0] = result[1][1] = 1;    result[0][1] = result[1][0] = 0;    return result;}

    Call that with something like:

    array_2x2 id22 = identity();

    Using vector<> is more common for this:

    #include <vector> // you need this in your includes

    // intvec_2d is a 2d vector (any size, not just 2x2)

    typedef std::vector<std::vector<int>> intvec_2d;

    intvec_2d vec_identity()

    {

        intvec_2d result = {{1, 0}, {0, 1}};    return result;

    }

    And you can call that with something like:

        intvec2d vid22 = vec_identity();

    Pretty much everything else about manipulating the result in the called function is the same as direct access to a local variable declared as a C array, std::array<> or std::vector<> type. 

    My recommendation is to use std::vector<> to make array-like objects unless you have a compelling reason to use one of the other types.

    • 3 weeks agoReport

      I recommend you stop trolling and giving bad answers.

  • Tasm
    Lv 6
    3 weeks ago

    Pass the address using the &. Look up passing by reference.

  • EddieJ
    Lv 7
    3 weeks ago

    Look at link in co-mment.  But, are you sure you really want to do that?

    • EddieJ
      Lv 7
      3 weeks agoReport

      https://stackoverflow.com/questions/8617683/return-a-2d-array-from-a-function

Still have questions? Get answers by asking now.