https://gcc.gnu.org/bugzilla/show_bug.cgi?id=122725
Bug ID: 122725
Summary: Allow functions returning a pointer to an array of
size determined by a parameter
Product: gcc
Version: 16.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: [email protected]
Target Milestone: ---
Pointers to array are useful, as they store the number of elements of the array
they hold, the type-system can be used to verify them --at least to some
extent--, and _Countof() can be used to get the number of elements
programmatically.
alx@devuan:~/tmp$ cat rap.c
int (*f(int n))[42];
int
main(void)
{
int (*p)[42] = f(42);
return _Countof(*p);
}
alx@devuan:~/tmp$ gcc -Wall -Wextra -S rap.c
alx@devuan:~/tmp$
One should be able to declare f() as
int (*f(int n))[n];
However, that would result in
$ gcc -Wall -Wextra -S rap.c
rap.c:1:17: error: ānā undeclared here (not in a function)
1 | int (*f(int n))[n];
| ^
That is unnecessary, and an expansion of the scope of function parameters until
the end of the declaration would be useful for this.
After all, we already allow pointers to array; we just don't allow it with
variable bounds, which are the more useful ones.