Why can't C functions return arrays? What was the rationale in the language design? [closed]

StackOverflow https://stackoverflow.com/questions/14513813

  •  17-01-2022
  •  | 
  •  

Question

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?

No correct solution

OTHER TIPS

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.

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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top