Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning multidimensional array from function

Tags:

How do I return a multidimensional array stored in a private field of my class?

class Myclass { private:    int myarray[5][5]; public:    int **get_array(); };  // This does not work: int **Myclass::get_array() {     return myarray; } 

I get the following error:

cannot convert int (*)[5][5] to int** in return

like image 933
justin Avatar asked Sep 15 '10 10:09

justin


People also ask

How do I return a two dimensional array from a function in C++?

Use Pointer to Pointer Notation to Return 2D Array From Function in C++ As an alternative, we can use a pointer to pointer notation to return the array from the function. This method has an advantage over others if the objects to be returned are allocated dynamically.

How do you return a matrix from a function?

You can "upgrade" the function by return the matrix like this: int *mat(int x, int y, const int *m_a, const int *m_b, int *m_out){ // do some work if (something is not OK) return NULL; else return m_out; } // in main() int a[5 * 6]; int b[5 * 6]; int result[5 * 6]; mat(5, 6, a, b, result); // use result.

Which function returns number of dimensions in the multidimensional array?

N = ndims( A ) returns the number of dimensions in the array A .


2 Answers

A two-dimensional array does not decay to a pointer to pointer to ints. It decays to a pointer to arrays of ints - that is, only the first dimension decays to a pointer. The pointer does not point to int pointers, which when incremented advance by the size of a pointer, but to arrays of 5 integers.

class Myclass { private:     int myarray[5][5]; public:     typedef int (*pointer_to_arrays)[5]; //typedefs can make things more readable with such awkward types      pointer_to_arrays get_array() {return myarray;} };  int main() {     Myclass o;     int (*a)[5] = o.get_array();     //or     Myclass::pointer_to_arrays b = o.get_array(); } 

A pointer to pointer (int**) is used when each subarray is allocated separately (that is, you originally have an array of pointers)

int* p[5]; for (int i = 0; i != 5; ++i) {     p[i] = new int[5]; } 

Here we have an array of five pointers, each pointing to the first item in a separate memory block, altogether 6 distinct memory blocks.

In a two-dimensional array you get a single contiguous block of memory:

int arr[5][5]; //a single block of 5 * 5 * sizeof(int) bytes 

You should see that the memory layout of these things are completely different, and therefore these things cannot be returned and passed the same way.

like image 77
visitor Avatar answered Nov 01 '22 18:11

visitor


There are two possible types that you can return to provide access to your internal array. The old C style would be returning int *[5], as the array will easily decay into a pointer to the first element, which is of type int[5].

int (*foo())[5] {    static int array[5][5] = {};    return array; } 

Now, you can also return a proper reference to the internal array, the simplest syntax would be through a typedef:

typedef int (&array5x5)[5][5]; array5x5 foo() {    static int array[5][5] = {};    return array; } 

Or a little more cumbersome without the typedef:

int (&foo())[5][5] {    static int array[5][5] = {};    return array; } 

The advantage of the C++ version is that the actual type is maintained, and that means that the actual size of the array is known at the callers side.

like image 45
David Rodríguez - dribeas Avatar answered Nov 01 '22 20:11

David Rodríguez - dribeas