Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C > Re: why cant fu...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 22 of 157 Topic 25604 of 27343
Post > Topic >>

Re: why cant functions return arrays

by Hallvard B Furuseth <h.b.furuseth@[EMAIL PROTECTED] > Apr 16, 2008 at 05:07 PM

"Bartc" <bc@[EMAIL PROTECTED]
> writes:

>"Hallvard B Furuseth" <h.b.furuseth@[EMAIL PROTECTED]
> wrote in message 
>news:hbf.20080415ngx4@[EMAIL PROTECTED]
> (...)
>>>>> int f()[10];
>>>>(...)
>>>> foo() { int *a; ... { ... a = f(); ... } }
>>> *a is a pointer to int. f() returns an array of int. So there is a
type
>>> mismatch and this should not compile, and the problem would not arise.
I
>>> don't think you can reasonably expect an array returned by a function
to
>>> 'decay' to a pointer in the same way as a normal array.
>>
>> No, I don't expect that - because the language forbinds "int f()[10]".
>> However, yes I do expect "a = <expression of type array of int>" to be
>> treated the same way regardless of where that expression comes from.
>> If it did not, just what should the program be allowed to do with it?
>
> Well, it could be assigned to an array:
>
> *a=f(); where a already points to a suitable space. Or possibly:

If you mean "int *a;", this is a type error (assigning an array to an
int) and I like compilers who warn me about that.

If a is declared as int (*a)[10], fine.  That's just a case of allowing
array assignment.  You can argue for or against that.  Arguments against
is that it can be limited or confusing due to the normal "decay to
pointer" feature, and also that passing an array by value can be a much
huger operation than it looks like when you are used to that what
actually gets passerd is the pointer.

> int b[10];
> b=f();

Same again.

> Or it could be passed directly to another function that takes arrays
> of the same size:
> g(f());

That may require that the program first copies the array to a tem****ary
array in the caller's stack.  So again, once it's there, what else is
the program allowed to do with the tem****ary?  E.g.:

>> Would &f() be OK?  *&f()?  f()[0]?  &f()[0]?

Another point - g(f()) would be an error if a prototype of g() was not
in scope.  Then the compiler would not know that g() was expecting
an array rather than a pointer to the first element.  So that answers
why it was not in K&R C - it did not have prototypes.

Current C does, but this feature would introduce another point where
calls with and without prototype would differ - and which could not
be fixed by casting the argument to the righe type.

>> To get this to work cleanly, the language basically needs a new class
of
>> types: "array which does not decay to a pointer".  Given that, one can
>> decide just how such a type can be used, and you'll also have the
>> answer to all of the above.
>
> Actually, adding such a new type of array wouldn't be that much of a
> problem. But the special interaction between newarrays and pointers
> wouldn't exist in the same way as for ordinary arrays.

They wouldn't anyway, due to the problem with g(f()) above.  With
regular "decay-to-pointer" arrays, that call works fine with or without
a prototype for g.

Defining this in terms of how functions work is the wrong approach.  It
makes things unnecessarily murky.

OTOH with such a non-decaying array type, we'd have that type clearly
spelling out the semantics of such array objects.  Which is what types
are _for_.  Jacob said he used operator overloading to make such an
array - and unless I misunderstood, what he made was a type.

> So setting up dynamic arrays and indexing them as though they were fixed
> arrays would not be possible.

I dunno.  C99 sup****ts variable-length arrays.  But what you are
suggesting also seems to be passing fixed-length arrays around, so I
don't quite see your point.

> So I don't see such newarrays being popular with C
> aficionados. Especially as these arrays would have to be a fixed size
> so would have the same limitations as early Pascal.
>
> A new array type needs something extra, and that would need to be
> flexible bounds, or bounds that are passed around with the array. But
> then, this would be taking C to a slightly different level.

Yes.  There are plenty of features it'd be nice to add to C.  The
problem is to choose between them, and to find ten people who agree on
which features would be good and which would be bad.  C99 was an attempt
at that, and has been less than successful so far.

-- 
Hallvard
 




 157 Posts in Topic:
why cant functions return arrays
"sanjay.vasudevan@[E  2008-04-14 14:25:11 
Re: why cant functions return arrays
roberson@[EMAIL PROTECTED  2008-04-14 21:41:05 
Re: why cant functions return arrays
jacob navia <jacob@[EM  2008-04-14 23:54:54 
Re: why cant functions return arrays
"Bartc" <bc@  2008-04-14 21:57:20 
Re: why cant functions return arrays
jacob navia <jacob@[EM  2008-04-14 23:59:17 
Re: why cant functions return arrays
richard@[EMAIL PROTECTED]  2008-04-14 22:56:12 
Re: why cant functions return arrays
"Default User"   2008-04-14 23:19:54 
Re: why cant functions return arrays
Eric Sosman <esosman@[  2008-04-15 08:44:29 
Re: why cant functions return arrays
"Dik T. Winter"  2008-04-15 01:15:20 
Re: why cant functions return arrays
"Dik T. Winter"  2008-04-15 01:12:54 
Re: why cant functions return arrays
Jack Klein <jackklein@  2008-04-14 21:59:34 
Re: why cant functions return arrays
jacob navia <jacob@[EM  2008-04-14 23:55:21 
Re: why cant functions return arrays
Jack Klein <jackklein@  2008-04-14 22:01:40 
Re: Wit-to-whine ratios
spinoza1111 <spinoza11  2008-05-18 00:09:20 
Re: why cant functions return arrays
user923005 <dcorbit@[E  2008-04-14 15:36:58 
Re: why cant functions return arrays
Ben Bacarisse <ben.use  2008-04-14 23:43:58 
Re: why cant functions return arrays
Hallvard B Furuseth <h  2008-04-15 01:00:20 
Re: why cant functions return arrays
"Bartc" <bc@  2008-04-14 23:55:07 
Re: why cant functions return arrays
Hallvard B Furuseth <h  2008-04-15 15:36:25 
Re: why cant functions return arrays
jacob navia <jacob@[EM  2008-04-15 16:25:56 
Re: why cant functions return arrays
"Bartc" <bc@  2008-04-15 17:42:24 
Re: why cant functions return arrays
Hallvard B Furuseth <h  2008-04-16 17:07:25 
Re: why cant functions return arrays
Shirsoft <shirsoft@[EM  2008-04-15 00:39:07 
Re: why cant functions return arrays
"Bartc" <bc@  2008-04-17 09:23:14 
Re: why cant functions return arrays
John Bode <john_bode@[  2008-04-15 12:25:29 
Re: why cant functions return arrays
jacob navia <jacob@[EM  2008-04-15 22:56:36 
Re: why cant functions return arrays
Eric Sosman <esosman@[  2008-04-15 21:25:36 
Re: why cant functions return arrays
"Dik T. Winter"  2008-04-16 10:42:53 
Re: why cant functions return arrays
Jack Klein <jackklein@  2008-04-15 20:53:13 
Re: why cant functions return arrays
CBFalconer <cbfalconer  2008-04-16 01:09:23 
Re: why cant functions return arrays
Keith Thompson <kst@[E  2008-04-15 13:07:04 
Re: why cant functions return arrays
Shirsoft <shirsoft@[EM  2008-04-16 04:49:02 
Re: why cant functions return arrays
CBFalconer <cbfalconer  2008-04-16 09:00:08 
Re: why cant functions return arrays
"Bartc" <bc@  2008-04-16 22:56:57 
Re: why cant functions return arrays
"Default User"   2008-04-16 23:05:17 
Re: why cant functions return arrays
"Bartc" <bc@  2008-04-17 00:12:45 
Re: why cant functions return arrays
"Default User"   2008-04-17 16:18:15 
Re: why cant functions return arrays
Antoninus Twink <nospa  2008-04-16 22:00:19 
Re: why cant functions return arrays
Richard <devr_@[EMAIL   2008-04-17 18:26:04 
Re: why cant functions return arrays
"sanjay.vasudevan@[E  2008-04-18 12:15:36 
Re: why cant functions return arrays
"sanjay.vasudevan@[E  2008-04-18 12:25:20 
Re: why cant functions return arrays
Keith Thompson <kst-u@  2008-04-19 20:32:07 
Re: why cant functions return arrays
James Dow Allen <jdall  2008-04-20 04:37:22 
Re: why cant functions return arrays
jacob navia <jacob@[EM  2008-04-20 14:23:30 
Re: why cant functions return arrays
Chris Torek <nospam@[E  2008-04-20 23:45:38 
Re: why cant functions return arrays
Richard <devr_@[EMAIL   2008-04-20 13:42:00 
Re: why cant functions return arrays
Richard <devr_@[EMAIL   2008-04-20 13:44:09 
Re: why cant functions return arrays
cri@[EMAIL PROTECTED] (R  2008-04-20 16:58:08 
Re: why cant functions return arrays
Richard Heathfield <rj  2008-04-20 17:07:58 
Re: why cant functions return arrays
gazelle@[EMAIL PROTECTED]  2008-04-20 17:35:07 
Re: why cant functions return arrays
Keith Thompson <kst-u@  2008-04-20 15:07:56 
Re: why cant functions return arrays
"Default User"   2008-04-21 00:49:39 
Re: why cant functions return arrays
CBFalconer <cbfalconer  2008-04-20 18:39:27 
Re: why cant functions return arrays
Richard <devr_@[EMAIL   2008-04-20 19:46:27 
Re: why cant functions return arrays
soscpd <soscpd@[EMAIL   2008-04-20 13:28:20 
Re: why cant functions return arrays
CBFalconer <cbfalconer  2008-04-20 18:43:28 
Re: why cant functions return arrays
"Bartc" <bc@  2008-04-20 23:53:55 
Re: why cant functions return arrays
Richard <devr_@[EMAIL   2008-04-21 02:51:43 
Re: why cant functions return arrays
Richard <devr_@[EMAIL   2008-04-21 03:01:04 
Re: why cant functions return arrays
soscpd <soscpd@[EMAIL   2008-04-20 18:02:38 
Re: why cant functions return arrays
Keith Thompson <kst-u@  2008-04-20 23:25:48 
Re: why cant functions return arrays
jacob navia <jacob@[EM  2008-04-21 10:36:08 
Re: why cant functions return arrays
Nick Keighley <nick_ke  2008-04-21 01:08:37 
Re: why cant functions return arrays
Ben Bacarisse <ben.use  2008-04-21 15:21:58 
Re: why cant functions return arrays
jacob navia <jacob@[EM  2008-04-21 17:21:05 
Re: why cant functions return arrays
Ben Bacarisse <ben.use  2008-04-21 18:09:56 
Wit-to-whine ratios
James Dow Allen <jdall  2008-04-24 02:15:53 
Re: Wit-to-whine ratios
Richard Heathfield <rj  2008-04-24 10:14:36 
Re: Wit-to-whine ratios
jacob navia <jacob@[EM  2008-04-24 12:36:15 
Re: Wit-to-whine ratios
pete <pfiland@[EMAIL P  2008-05-08 04:57:34 
Re: Wit-to-whine ratios
pete <pfiland@[EMAIL P  2008-05-08 05:18:48 
Re: Wit-to-whine ratios
gazelle@[EMAIL PROTECTED]  2008-05-08 12:34:27 
Re: why cant functions return arrays
John Bode <john_bode@[  2008-04-24 08:13:31 
Re: Wit-to-whine ratios
Antoninus Twink <nospa  2008-04-24 20:44:43 
Re: Wit-to-whine ratios
spinoza1111 <spinoza11  2008-05-08 00:39:32 
Re: Wit-to-whine ratios
spinoza1111 <spinoza11  2008-05-08 00:47:55 
Re: Wit-to-whine ratios
spinoza1111 <spinoza11  2008-05-08 00:51:05 
Re: Wit-to-whine ratios
Flash Gordon <spam@[EM  2008-05-08 19:03:52 
Re: Wit-to-whine ratios
Richard Heathfield <rj  2008-05-08 20:03:38 
Re: Wit-to-whine ratios
spinoza1111 <spinoza11  2008-05-08 00:58:09 
Re: why cant functions return arrays
spinoza1111 <spinoza11  2008-05-08 04:12:50 
Re: Wit-to-whine ratios
spinoza1111 <spinoza11  2008-05-08 05:50:08 
Re: Wit-to-whine ratios
Mike <m.fee@[EMAIL PRO  2008-05-12 14:00:37 
Re: Wit-to-whine ratios
CBFalconer <cbfalconer  2008-05-08 17:48:12 
Re: Wit-to-whine ratios
spinoza1111 <spinoza11  2008-05-08 22:46:48 
Re: Wit-to-whine ratios
Richard Heathfield <rj  2008-05-09 06:07:25 
Re: Wit-to-whine ratios
jacob navia <jacob@[EM  2008-05-09 09:21:55 
Re: Wit-to-whine ratios
Richard Heathfield <rj  2008-05-09 07:50:04 
Re: Wit-to-whine ratios
jacob navia <jacob@[EM  2008-05-09 09:58:12 
Re: Wit-to-whine ratios
Richard Heathfield <rj  2008-05-09 08:32:08 
Re: Wit-to-whine ratios
Mark McIntyre <markmci  2008-05-10 00:39:54 
Re: Wit-to-whine ratios
CBFalconer <cbfalconer  2008-05-09 23:58:02 
Re: Wit-to-whine ratios
Mark McIntyre <markmci  2008-05-10 12:06:49 
Re: Wit-to-whine ratios
Richard Heathfield <rj  2008-05-10 04:38:49 
Re: Wit-to-whine ratios
Flash Gordon <spam@[EM  2008-05-09 18:53:39 
Re: Wit-to-whine ratios
Flash Gordon <spam@[EM  2008-05-09 18:55:58 
Re: Wit-to-whine ratios
Mark McIntyre <markmci  2008-05-10 00:48:00 
Re: Wit-to-whine ratios
Chris McDonald <chris@  2008-05-10 00:14:11 
Re: Wit-to-whine ratios
CBFalconer <cbfalconer  2008-05-10 00:01:28 
Re: Wit-to-whine ratios
spinoza1111 <spinoza11  2008-05-09 18:32:09 
Re: Wit-to-whine ratios
Richard Heathfield <rj  2008-05-10 05:03:31 
Re: Wit-to-whine ratios
spinoza1111 <spinoza11  2008-05-10 10:17:24 
Re: Wit-to-whine ratios
Richard Heathfield <rj  2008-05-10 18:12:25 
Re: Wit-to-whine ratios
Walter Banks <walter@[  2008-05-10 14:55:53 
Re: Wit-to-whine ratios
spinoza1111 <spinoza11  2008-05-10 12:09:50 
Re: Wit-to-whine ratios
Richard Heathfield <rj  2008-05-10 21:08:47 
[very OT] meds
"Malcolm McLean"  2008-05-11 21:09:26 
Re: Wit-to-whine ratios
Nick Keighley <nick_ke  2008-05-10 15:16:08 
Re: Wit-to-whine ratios
spinoza1111 <spinoza11  2008-05-10 22:35:29 
Re: Wit-to-whine ratios
Richard Heathfield <rj  2008-05-11 06:57:45 
Re: Wit-to-whine ratios
pete <pfiland@[EMAIL P  2008-05-11 06:37:23 
Re: Wit-to-whine ratios
pete <pfiland@[EMAIL P  2008-05-11 08:59:08 
Re: Wit-to-whine ratios
spinoza1111 <spinoza11  2008-05-10 22:38:40 
Re: Wit-to-whine ratios
James Dow Allen <jdall  2008-05-11 03:06:34 
Re: Wit-to-whine ratios
James Dow Allen <jdall  2008-05-11 03:15:58 
Re: Wit-to-whine ratios
"Default User"   2008-05-11 20:10:58 
Re: Wit-to-whine ratios
richard@[EMAIL PROTECTED]  2008-05-11 22:46:49 
Re: Wit-to-whine ratios
CBFalconer <cbfalconer  2008-05-11 07:14:49 
Re: why cant functions return arrays
spinoza1111 <spinoza11  2008-05-11 13:14:14 
Re: why cant functions return arrays
Mark McIntyre <markmci  2008-05-11 23:41:36 
Re: why cant functions return arrays
spinoza1111 <spinoza11  2008-05-11 13:18:17 
Re: why cant functions return arrays
spinoza1111 <spinoza11  2008-05-11 13:22:42 
Re: why cant functions return arrays
spinoza1111 <spinoza11  2008-05-11 13:23:53 
Re: Wit-to-whine ratios
Eligiusz Narutowicz<el  2008-05-11 22:29:00 
Re: Wit-to-whine ratios
spinoza1111 <spinoza11  2008-05-11 13:40:10 
Re: Wit-to-whine ratios
Mike <m.fee@[EMAIL PRO  2008-05-12 14:21:48 
Re: Wit-to-whine ratios
pete <pfiland@[EMAIL P  2008-05-12 08:04:49 
Re: Wit-to-whine ratios
santosh <santosh.k83@[  2008-05-12 19:08:19 
Re: Wit-to-whine ratios
Colonel Sanders <colon  2008-05-13 12:54:45 
Re: why cant functions return arrays
spinoza1111 <spinoza11  2008-05-11 13:41:03 
Re: meds
spinoza1111 <spinoza11  2008-05-11 13:48:30 
Re: meds
Richard Heathfield <rj  2008-05-12 01:00:36 
Re: meds
"rio" <a@[EM  2008-05-12 11:34:34 
Re: meds
santosh <santosh.k83@[  2008-05-12 16:43:36 
Re: meds
Richard Heathfield <rj  2008-05-12 11:28:28 
[ot] s C otch
pete <pfiland@[EMAIL P  2008-05-12 07:44:15 
Re: meds
"rio" <a@[EM  2008-05-12 17:52:35 
Re: meds
santosh <santosh.k83@[  2008-05-12 21:40:36 
Re: Wit-to-whine ratios
Chad <cdalten@[EMAIL P  2008-05-11 20:40:21 
apologies to clc [Was: Wit-to-whine ratios]
Nick Keighley <nick_ke  2008-05-12 01:45:39 
Re: meds
spinoza1111 <spinoza11  2008-05-12 04:40:54 
Re: meds
Nick Keighley <nick_ke  2008-05-12 04:44:08 
Re: meds
Eligiusz Narutowicz<el  2008-05-12 13:44:43 
Re: meds
santosh <santosh.k83@[  2008-05-12 19:02:43 
Re: Wit-to-whine ratios
spinoza1111 <spinoza11  2008-05-12 04:56:53 
Re: Wit-to-whine ratios
Mike <m.fee@[EMAIL PRO  2008-05-14 10:12:44 
Re: meds
Eligiusz Narutowicz<el  2008-05-12 15:34:43 
Re: meds
santosh <santosh.k83@[  2008-05-12 19:32:30 
Re: meds
Richard Heathfield <rj  2008-05-12 14:45:30 
Re: meds
santosh <santosh.k83@[  2008-05-12 21:44:15 
Re: meds
Eligiusz Narutowicz<el  2008-05-12 16:19:06 
Re: meds
santosh <santosh.k83@[  2008-05-12 21:42:21 
Re: meds
Sri Harsha Dandibhotla &l  2008-05-12 20:57:21 
Re: meds
Richard Heathfield <rj  2008-05-13 04:05:21 
Re: meds
Sri Harsha Dandibhotla &l  2008-05-12 21:10:31 
Re: meds
Richard Heathfield <rj  2008-05-13 04:36:13 
Re: meds
Eligiusz Narutowicz<el  2008-05-13 16:32:23 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Sep 6 21:49:01 CDT 2008.