A function dynamically creates an int array whose elements are predetermined to be int[2]. Is there any way to have a function assign values to that array and then return it to the caller.
The below code accomplishes this task but throws warning: initialization from incompatible pointer type [enabled by default]
#include <stdlib.h>
#include <stdio.h>
int *get_values()
{
int (*x)[2] = malloc(sizeof(int[2])*3);
x[0][0] = 1;
x[0][1] = 2;
x[1][0] = 11;
x[1][1] = 12;
x[2][0] = 21;
x[2][1] = 22;
return x;
}
int main()
{
int (*x)[2] = get_values();
int i;
for (i=0; i!=3; i++)
printf("x[%d] = { %d, %d }\n", i, x[i][0], x[i][1]);
}
I'm aware of the alternative where you dynamically allocate both dimensions, but this is something that I am curious about.
int (*get_values())[2] { ... }.