0

I was trying to return 2D integer array from a function in C. I was able to return it using dynamic memory allocation using malloc() but I am unable but curious to understand how can we use static keyword to do it.

Below is the code snippet which successfully returns 2D array using static keyword,

int (*(get_2d_arr_using_static)())[10]    // Can someone explain me this statement in detail ?
{
    static int arr[10][10] = { { 1,2,3}, {4,5,6}, {7,8,9}};
    return arr;
}

int main()
{
  int (*arr)[10] =  get_2d_arr_using_static();  // Need some insights on this statement too 
  printf("Result ( x: 3, y =3 ): \n");
  for (int i = 0; i < 3; i++)
  {
    for (int j = 0; j < 3; j++)
    {
      printf(" %d", arr[i][j]);
    }
    printf("\n");
  }
}

I need some explanation on the commented statements.

8
  • 3
    Why is this tagged c++? This is plain C and not at all how you'd approach it in C++. The two languages are very different. Commented May 30, 2020 at 16:19
  • 1
    Handy tool: cdecl.org Commented May 30, 2020 at 16:27
  • I did so because most of the C++ programmers have C experience and I want this question to reach them as well. Secondly, this code give exact output with gcc and g++. So technically, it is valid C++ code as well. Commented May 30, 2020 at 16:29
  • A good way to ask a question like this is write what you think it does, state your reasoning, and thenask if you are right. If you'rte wrong, someone will correct you. If you're right, excellent. Someone will probably provide more details or caveats and you're good to go. Commented May 30, 2020 at 16:29
  • @user4581301, thanks, it is really a helpful tool. Commented May 30, 2020 at 16:31

1 Answer 1

1

I was trying to return 2D integer array from a function in C.

There's the problem. A function cannot return an array in C.

You can return pointers though, and pointers may point to an element of an array.

That array may be allocated dynamically or statically. Returning a pointer to an automatic array declared within the function would be pointless because the lifetime of that array ends when it goes out of scope, so you would be returning a dangling pointer.

int (*(get_2d_arr_using_static)())[10]    // Can someone explain me this statement in detail ?

This declares a function that returns a pointer to an array of 10 integers.

It should be noted that int[10][10] is an array of 10 arrays of 10 integers. As such, element of that array is an array of 10 integers, and the function returns a pointer to such element.

int (*arr)[10] =  get_2d_arr_using_static();  // Need some insights on this statement too 

This calls that function and initialises a pointer to an array of 10 integers.


It is possible to return an array by copy as well, but indirectly. It can be achieved by wrapping the array in a struct:

struct wrapper {
    int arr[10][10];
};


struct wrapper foo(void) {
     struct wrapper w;
     // fill the array or something
     return w;
}

Since you tagged C++, I'll mention that C++ standard library has a template for such wrapper class: std::array.

Sign up to request clarification or add additional context in comments.

2 Comments

A quick note: Returning a statically allocated array means there is one array for the entire program. Every caller to get_2d_arr_using_static gets the same array. This is very different from the dynamically allocated version that can (and almost certainly does) return a different array for every caller. This can lead to huge problems in a multithreaded system or if the returned pointer is cached and used in multiple places in the code.
@user4581301 More generally, static storage is global state, and should be avoided.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.