The question might appear incredibly simple but how do I declare a function in c that returns a double array. For instance
char myfunction(int a, int b)
{
...
char d[a][b];
...
return d;
}
But it doesn’t seem to work (if the response could avoid the ‘malloc’ and ‘free’ usage, it could be more helpful to understand those kinds of basic usage)
Best,
Newben
Well, you can’t because you are proposing to return a pointer which is invalid once the function returns. The array is declared with automatic storage duration and, as a result, all references to it are invalid once the function returns. You cannot return arrays from functions in C.
If you need to populate data in the function then either return a
char**that youmallocfrom within the function.You can now use multi-dimensional array syntax on the return value.