In C arrays are second class citizens. In order to pass them around you would need to wrap in a structure.
I.e.:
struct t_array42int{int o[4][2];} getValid(int *blank){
int row = blank[0];
int col = blank[1];
static struct t_array42int valid_container;
int (*valid)[2] = valid_container.o;
valid[0][0] = row;
valid[0][1] = col-1;
valid[1][0] = row;
valid[1][1] = col+1;
valid[2][0] = row-1;
valid[2][1] = col;
valid[3][0] = row+1;
valid[3][1] = col;
return valid_container;
}
Then you can store your function return value like:
struct t_array42int tmp = getValid(something);
Of-course there are ways to simplify this process of "wrapping" but those are often not standard or too obfuscated.
The disadvantage of using structures is that structure types aren't compared by their members layout but instead (normally) by their names. So something like this:
struct { int o[2]; } a;
struct { int o[2]; } b;
a = b; //Error
Is illegal.
You should write:
struct t_array2int { int o[2]; } a;
struct t_array2int b;
a = b; //OK
Instead.
You could also return a pointer to your array instead because it's storage is static (i.e. it won't cease to exist after returning from function):
int (*getValid(int *blank))[2]{
int row = blank[0];
int col = blank[1];
static int valid[4][2];
valid[0][0] = row;
valid[0][1] = col-1;
valid[1][0] = row;
valid[1][1] = col+1;
valid[2][0] = row-1;
valid[2][1] = col;
valid[3][0] = row+1;
valid[3][1] = col;
return valid;
}
The function prototype:
int (*getValid(int *blank))[2]
Means that if you call your function getValid
with a single argument of type int *
- getValid(something)
and dereference it, *getValid(something)
you'll get an array that can't be accessed like (*getValid(something))[2]
and so have maximum 2 elements of type int
.
Simple instance of the above example will be:
int (*tmp)[2] = getValid(something);
And I also suggest to write your array parameters with their real type (which can never be an array - the compiler is only fooling you into thinking that arrays can be passed by value). I.e.: void f(int a[N])
is adjusted to void f(int *a)
. And int *
is not the same as int [N]
.
For a newcomer I suggest you remember that arrays are not the same as pointers but are mistreated as such very badly. Also I suggest you to never write functions with array parameters. And if you want to use arrays in a different way then to get pointer to their first element to use structures instead - much safer.
The main thing with array parameter is that:
void f(int a[2])
{
sizeof(a) == sizeof(int *); //evaluates to 1
sizeof(a) != sizeof(int [2]); //evaluates to 1
}