Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't C functions return an array type? [duplicate]

I am new to C language and am wondering:

Why can't C functions return an array type?

I know that an array name is an address to the first value of the array, and arrays are second-class citizens in C.

like image 324
user3297620 Avatar asked Sep 17 '15 00:09

user3297620


2 Answers

You answered the question yourself already: arrays are second-class citizens.

C returns by value. Arrays cannot be passed by value, therefore they cannot be returned.

As to why arrays cannot be passed by value: this was a design decision made by K&R when they were first designing the language, and it's too late to change it now because all the existing C code would break.

like image 162
M.M Avatar answered Oct 19 '22 07:10

M.M


Based on The Development of the C Language, it looks like the real reason is mostly related to the evolution of C from B and BCPL. B didn't have array variables, all it had were pointers. It didn't have structures, either. When structures were added to C, and they were allowed to contain array members, he needed a way to handle these enclosed arrays similarly to the way the B-style arrays were handled, and the solution was to have arrays convert to pointers whenever they're used in expressions. Here's the section of the paper explaining this.

Problems became evident when I tried to extend the type notation, especially to add structured (record) types. Structures, it seemed, should map in an intuitive way onto memory in the machine, but in a structure containing an array, there was no good place to stash the pointer containing the base of the array, nor any convenient way to arrange that it be initialized. For example, the directory entries of early Unix systems might be described in C as

struct {
     int inumber;
     char name[14];
  };

I wanted the structure not merely to characterize an abstract object but also to describe a collection of bits that might be read from a directory. Where could the compiler hide the pointer to name that the semantics demanded? Even if structures were thought of more abstractly, and the space for pointers could be hidden somehow, how could I handle the technical problem of properly initializing these pointers when allocating a complicated object, perhaps one that specified structures containing arrays containing structures to arbitrary depth?

The solution constituted the crucial jump in the evolutionary chain between typeless BCPL and typed C. It eliminated the materialization of the pointer in storage, and instead caused the creation of the pointer when the array name is mentioned in an expression. The rule, which survives in today’s C, is that values of array type are converted, when they appear in expressions, into pointers to the first of the objects making up the array.

This isn't specific to passing arrays to and from functions, it applies any time an array is used in an expression (except when it's the argument of the & operator).

Note also that this design allows array variables and memory allocated dynamically with malloc() to be treated equivalently when passed to function.

like image 27
Barmar Avatar answered Oct 19 '22 08:10

Barmar