Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return a 2d array from a function

Tags:

c

I am a newbie to C. I am trying to return a 2d array from a function. It is something like this

int *MakeGridOfCounts(int Grid[][6])
{
  int cGrid[6][6] = {{0, }, {0, }, {0, }, {0, }, {0, }, {0, }};
  int (*p)[6] = cGrid;
  return (int*)p;
}

I know this causes an error, need help. thanks

like image 295
user1047092 Avatar asked Dec 23 '11 15:12

user1047092


People also ask

How can a function return a 2D array?

The show() method is declared with a two dimensional array as its argument. The purpose of this method is to print elements of array passed to it. The sum() method is declared with two 2D arrays as its arguments and made to return a two dimensional array.

How do you return an array from a function?

C programming does not allow to return an entire array as an argument to a function. However, you can return a pointer to an array by specifying the array's name without an index.

How do I return a 2D array in Python?

In Python, we can access elements of a two-dimensional array using two indices. The first index refers to the indexing of the list and the second index refers to the position of the elements. If we define only one index with an array name, it returns all the elements of 2-dimensional stored in the array.

Can we return 2D array in C++?

Similarly, we can pass two-dimensional arrays in C++. C++ does not allow us to pass an entire array as an argument to a function. However, we can pass a pointer to an array by specifying the array's name without an index. For passing a 2D array to a function, we have three ways.


1 Answers

The C language has a basic flaw: it is impossible to return arrays from functions. There are many workarounds for this; i'll describe three.

Replace by a pointer to an array

Return a pointer instead of an array itself. This leads to another problem in C: when a function returns a pointer to something, it should usually allocate the something dynamically. You should not forget to deallocate this later (when the array is not needed anymore).

typedef int (*pointer_to_array)[6][6];

pointer_to_array workaround1()
{
    pointer_to_array result = malloc(sizeof(*result));
    (*result)[0][0] = 0;
    (*result)[1][0] = 0;
    (*result)[2][0] = 0;
    (*result)[3][0] = 0;
    (*result)[4][0] = 0;
    (*result)[5][0] = 0;
    return result;
}

Replace by a pointer to int

A 2-D array appears just as a sequence of numbers in memory, so you can replace it by a pointer to first element. You clearly stated that you want to return an array, but your example code returns a pointer to int, so maybe you can change the rest of your code accordingly.

int *workaround2()
{
    int temp[6][6] = {{0}}; // initializes a temporary array to zeros
    int *result = malloc(sizeof(int) * 6 * 6); // allocates a one-dimensional array
    memcpy(result, temp, sizeof(int) * 6 * 6); // copies stuff
    return result; // cannot return an array but can return a pointer!
}

Wrap with a structure

It sounds silly, but functions can return structures even though they cannot return arrays! Even if the returned structure contains an array.

struct array_inside
{
    int array[6][6];
};

struct array_inside workaround3()
{
    struct array_inside result = {{{0}}};
    return result;
}
like image 150
anatolyg Avatar answered Sep 30 '22 02:09

anatolyg