Re: why cant functions return arrays
- From: Jack Klein <jackklein@xxxxxxxxxxx>
- Date: Tue, 15 Apr 2008 20:53:13 -0500
On Tue, 15 Apr 2008 12:25:29 -0700 (PDT), John Bode
<john_bode@xxxxxxxxxxx> wrote in comp.lang.c:
On Apr 14, 10:01 pm, Jack Klein <jackkl...@xxxxxxxxxxx> wrote:
On Mon, 14 Apr 2008 23:55:21 +0200, jacob navia <ja...@xxxxxxxxxx>
wrote in comp.lang.c:
sanjay.vasude...@xxxxxxxxx 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.
In the earliest days of C, you could not do structure assignment,
either. You had to memcpy().
To tell you the truth, I don't think that is as big of an issue as
breaking existing code. This is caused mainly by the interchangeable
use of array names and pointers in expressions, and particularly in
function calls.
Consider:
int ia [10];
int *ip = ia;
....then all of these are valid ways of referring the same int:
*ia; /* looks like ia is a pointer */
ia[0];
*ip;
ip[0];
But the bigger issue, at least prior to prototypes being added by
standardization, was matching function calls to definitions. Look at:
int some_func();
int main()
{
int array [10] = { 0 };
some_func(a);
return 0;
}
In the absence of a prototype declaration (and the sample above with
the non-prototype declaration is legal from K&R all the way through
C99), what gets passed to some_func, a pointer to int or an array of
10 ints?
Since there were no prototypes in K&R, there was no way to inform the
compiler which some_func was actually expecting.
The C89/90 standard could have added passing arrays, classing
functions that received them as requiring correct prototypes in scope
at all calls, under the penalty of undefined behavior. They did that
with variadic functions, and indeed all functions that have arguments
that can't be generated by the default promotions.
But it would have added a feature to the language with difficult
syntax and a great chance of programmer mistakes.
On top of that, the opposite side of the coin is the utility gained by
programmers for all the pain to the implementers.
How useful are assignments, parameters, or return types that are
fixed-size arrays of specific types? That was how Pascal was
originally designed, and it was one of the issues that side tracked
that language until conformant arrays were added to an extended
standard.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
.
- Follow-Ups:
- Re: why cant functions return arrays
- From: CBFalconer
- Re: why cant functions return arrays
- References:
- why cant functions return arrays
- From: sanjay.vasudevan@xxxxxxxxx
- Re: why cant functions return arrays
- From: jacob navia
- Re: why cant functions return arrays
- From: Jack Klein
- Re: why cant functions return arrays
- From: John Bode
- why cant functions return arrays
- Prev by Date: Re: How to eliminate this global variable, silent?
- Next by Date: Re: How to eliminate this global variable, silent?
- Previous by thread: Re: why cant functions return arrays
- Next by thread: Re: why cant functions return arrays
- Index(es):
Relevant Pages
|