I want a function to return a pointer to an array of size 10. What is the prototype for such a function?
I have tried 3 different prototypes and it doesn't work:
int(*)[10] returnPtrArray(int (*arr)[10])
{
return arr;
}
//The return type doesn't seem to work
int(*)[10] returnPtrArray(int[][10]);
int (returnPtrArray(int[][10])(*)[10];
int(*)(returnPtrArray(int[][10])[10];
//none of these prototypes seem to work
//calling
int main()
{
int a[5][10];
int (*ptr)[10] = returnPtrArray(&a);
//How do I make this work?
}
The correct definition of the function would be:
int (*returnPtrArray(int (*arr)[10]))[10]
{
return arr;
}
Breaking this down: returnPtrArray
is a function:
returnPtrArray()
That takes a pointer to an array of 10 int
:
returnPtrArray(int (*arr)[10])
And returns a pointer:
*returnPtrArray(int (*arr)[10])
To an array of size 10:
(*returnPtrArray(int (*arr)[10]))[10]
Of int
:
int (*returnPtrArray(int (*arr)[10]))[10]
And you would call it like this:
int a[5][10];
int (*ptr)[10] = returnPtrArray(a);