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.