19

I am a beginning programmer and know little about C. One thing I know but have not had explained to me is why functions can't return arrays. This is easily circumvented (Return a 2d array from a function) by use of pointers or by wrapping the array in a struct, but my question is "Why?".

Why can't functions return arrays?

I am aware that there are easy workarounds, but the question has been bugging me ever since I discovered it. Is this a flaw in C's internal design, or was this put there on purpose? Is there a particular reason that this limitation exists?

Community
  • 1
  • 1
fouric
  • 1,598
  • 1
  • 19
  • 37
  • I assume you mean why doesn't C allow returning arrays **by value** that is a called function creates an array on the stack and returns it, resulting in a copy of what was on the stack similar to how other basic types work, correct? – Doug T. Jan 25 '13 at 01:18
  • 5
    Whats up with the close vote? Seriously this is an interesting question about the design of C – Doug T. Jan 25 '13 at 01:19
  • 3
    Because arrays aren't assignable. So even if you could return an array, you couldn't do much with it. – Oliver Charlesworth Jan 25 '13 at 01:19
  • 2
    @OliCharlesworth So I guess the question could be better rephrased as: "Why aren't arrays assignable?" – Mysticial Jan 25 '13 at 01:20
  • 6
    @DougT. because questions of the form" why was x designed like y?" can't be answered objectively and definitively, generally. Unless the responder is the author of the language. – Oliver Charlesworth Jan 25 '13 at 01:21
  • 2
    The same reason we chose '\0' as string terminator instead of coding a length into it. Because someone decided to and that's how it's been ever since. – Jesus Ramos Jan 25 '13 at 01:22
  • I would argue arrays don't actually exist in C. They're just pointers - pointers that the compiler knows how to dereference in a sequential way, but `int[] a` can be dereferenced with `*a` just as well as `a[0]`. – FrankieTheKneeMan Jan 25 '13 at 01:24
  • 2
    @OliCharlesworth that's only partially true. The authors of the language speak/write widely about their design decisions. One can always do a little research and potentially find rationale behind design decisions. – Doug T. Jan 25 '13 at 01:24
  • 2
    Duplicate of http://stackoverflow.com/questions/5157439/why-doesnt-c-support-functions-returning-arrays ? There's a really elaborate answer there. – Mike Trusov Jan 25 '13 at 01:24
  • 1
    @DougT. maybe, maybe not. But either way, it's not a question about a " practical programming problem that you face" or whatever the faq says. – Oliver Charlesworth Jan 25 '13 at 01:25
  • @OliCharlesworth I agree it may be better suited for programmers SE. – Doug T. Jan 25 '13 at 01:26
  • 1
    @FrankieTheKneeMan: No, arrays *are not* pointers. They are a distinct set of object types. There are some odd things that happen to expressions of array type in most contexts. Section 6 of the [comp.lang.c FAQ](http://www.c-faq.com/) explains the issues very well. – Keith Thompson Jan 25 '13 at 01:28
  • @KeithThompson Right, I forgot about all that jiggery pokery. – FrankieTheKneeMan Jan 25 '13 at 01:30
  • 1
    @FrankieTheKneeMan: An array isn't the pointer; it's the block of ints, chars, or whatever that that pointer points to. Just so happens that the compiler knows where that memory is, and passing stuff by pointer is insanely common, which is why arrays *decay* into pointers so easily. – cHao Jan 25 '13 at 01:32
  • @cHao, Keith Reminded me of that. I'd still argue that arrays aren't special in anyway, they're just side by side blocks of data. So many of us young'ns have been conditioned by the way that Object Oriented Languages that having a language construct entails a bunch of extra stuff (such as length monitoring, being able to pass it in and out of functions, etc). The way C sees an array is fundamentally different than the way Java, or Python sees it. And the term has evolved with the languages. – FrankieTheKneeMan Jan 25 '13 at 01:47
  • @Frankie: Well, so many of you young'uns weren't raised right. :) The term hasn't evolved; it still means the same thing it always has. Even in most OO languages, you actually pass and return *references* to arrays rather than the actual array. C's just less sneaky about it. :) – cHao Jan 25 '13 at 02:12

2 Answers2

0

Speculation...

  • arrays are expected to be large (or at least not small), so copying array to allow "value type" behavior will take non-trivial amount of time. One would expect = to copy arrays thus introducing hidden slowness in the code which was never explicit goal of C.
  • arrays in C do not include size anywhere - there is just no easy technical way to implement copy as value for current concept of "array" as it exists in the language.
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
0

I am not sure, but i think.

  1. C is developed when memory was constraint.
  2. Someone might have told alternative, Instead of passing full array back, we can return pointer, so why to provide other not-so-better alternative.
Raxit Sheth
  • 2,491
  • 5
  • 17
  • 20