I have tried this but it won\'t work:
#include
int * retArr()
{
int a[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
return a;
}
Several issues:
First of all, you cannot initialize an array from a function call; the language definition simply doesn't allow for it. An array of char may be initialized with a string literal, such as
char foo[] = "This is a test";
an array of wchar_t, char16_t, or char32_t may be initialized with a wide string literal, but otherwise the initializer must be a brace-enclosed list of values.
Secondly, you have a type mismatch; except when it is the operand of the sizeof or unary & operators, or is a string literal being used to initialize another array in a declaration, an expression of type "N-element array of T" will be converted to an expression of type "pointer to T", and the value of the expression will be the address of the first element;
In the function retArr, the type of a in the expression is "3-element array of 3-element array of int"; by the rule above, this will be converted to an expression of type "pointer to 3-element array of int", or int (*)[3]:
int (*retArr())[3]
{
int a[3][3] = ...;
return a;
}
but as Brendan points out, once retArr exits a no longer exists; the pointer value that is returned winds up being invalid.