EDIT: the correct syntax for declaring a function with the return type of array pointer in the example I wrote further below in my code example section should be as follows:
int (*my_function(void))[10]
{
...
}
Important note for future readers who might come across the same question: don't typecast malloc! Just don't. Thanks to user3386109 for that one. Further reading on the why.
Thanks to Andrew Henle and Eric Postpischil for their answers. Here's a really good explanation if anyone wants to read further on the the topic. Original thread below:
What I want to know
How to return a pointer to array in C.
Why I want to know that
Because it (is?) should be possible to do so.
For what I want it
To return multidimensional arrays from functions.
"Have you tried structs?"
Yes, but this isn't about structs. The point (no pun intended) of this thread is doing it with pointers.
Code example
My example is wrong, thus the reason I'm asking here, but here it go:
int (*)[10]my_function(void) // Should be returning a type of array pointer to 10-elements array. Sure enough it's not.
{
int (*ptr)[10] = (int (*)[10]) malloc(10 * sizeof(int));
return ptr;
}
int main(void)
{
printf("Address of ptr is: %d", my_function());
return 0;
}
As you can see, I'm not sure about how to typecast malloc to type array pointer to 10-elements array as well. If you know how, please let me know.
IMPORTANT NOTE
As far as I know, using double pointers (pointer to pointer) in this case is wrong. Code example:
int **my_function(void); // WRONG, afaik
Rationale: this video, at 16:22.
I already tried searching online and in some books, they have examples using structs, pointer to array as function arguments, but not this.
If you have some knowledge about this topic, please reply, I'll be grateful.