Re: why cant functions return arrays
On Apr 14, 10:01 pm, Jack Klein <jackkl...@spamcop.net> wrote:
> On Mon, 14 Apr 2008 23:55:21 +0200, jacob navia <ja...@nospam.com>
> wrote in comp.lang.c:
>
> > sanjay.vasude...@gmail.com wrote:
> > > Why are the following declarations invalid in C?
>
> > > int f()[];
> > > int f()[10];
>
> > > It would be great if anyone could also explain the design decision
> > > for such a language restricton.
>
> > > Regards,
> > > Sanjay
>
> > There is no reason. It is just a design mistake of the language.
>
> That is, in the opinion of the very opinionated Jacob Navia.
>
> I happen to think it was a good choice. For about 30 years now you
> have been able to pass and return fixed-size arrays by value by
> wrapping them in a structure.
>
But that just brings up the question, why can you return structs
containing arrays by value, but not arrays themselves? Surely any
*technical* issue for one is just as valid for the other; so why the
difference?
I *suspect* the reason behind the OP's query is that array objects are
non-modifiable lvalues:
int a[10], b[10];
b = a; /* illegal! */
Since you can't assign a new array value to an array type object, it
doesn't make sense to allow functions to return array types. That
would make it not a mistake in design for no good reason (per Jacob),
but a consequence of how arrays are handled by the rest of the
language. Whether "how arrays are handled by the rest of the
language" is a mistake in design or not is an open issue.
|