Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why you cannot return fixed size / const array in C? [closed]

Tags:

arrays

c

pointers

I wonder why it is not possible to return array in C? After all, array is just a pointer backed by size info (to make sizeof work). First I thought this was done to prevent me from returning array defined on my stack, but nothing prevents me from returning pointer to something on my stack (gcc warns me, but code compiles). And I also can return string literal which is statically storaged array of chars. By the way, in lunux it is stored in .rodata, and const array is stored there also (check it with objdump), so I can return array (casting it to pointer) and it works, but AFAIK this is just implementation-specific (another OS/Compiler may store const on stack).

I have 2 ideas how to implement array returning: Just copy it as value (as it is done for structure. I even can return array wrapping it into structure!!), and create pointer to it automatically or allow user to return const array and create contract that such array should have static storage duration (as it done for strings). Both ideas are trivial! So, my question is why K&R did not implement something like that?

like image 415
user996142 Avatar asked Mar 16 '15 22:03

user996142


People also ask

Do arrays have fixed size in C?

Arrays a kind of data structure that can store a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.

Are array sizes always fixed?

Arrays are fixed size. Once we initialize the array with some int value as its size, it can't change.

Can you return an array in C?

C programming does not allow to return an entire array as an argument to a function. However, you can return a pointer to an array by specifying the array's name without an index.

Can you modify a const array C++?

You can't. The const keyword is used to create a read only variable. Once initialised, the value of the variable cannot be changed but can be used just like any other variable. That means, a const qualified variable must not be assigned to in any way during the run of a program.


1 Answers

Technically, you can return an array; you just can't do it "directly", but have to wrap it in a struct:

struct foo {
    int array[5];
};

struct foo returns_array(void) {
    return((struct foo) {
        .array = {2, 4, 6, 8, 10}
    });
}

Why C doesn't allow you to do it directly even though it has the ability is still a good question, though. It is probably related to the fact that it doesn't support whole-array assignments either:

void bar(int input[5]) {
    int temp[5];

    temp = input;   <-- Doesn't compile
}

What makes it even stranger though, of course, is that whole-array copy via argument-passing is supported. If someone knows how to find the ANSI committee's decisions on the matter, that would be interesting to read.

However,

After all, array is just a pointer backed by size info (to make sizeof work).

This is not correct. There is no explicit pointer, nor any stored size, of an array. The array is stored as the raw values, packed together; the size is only known inside the compiler and never made explicit as run-time data in the program. The array decays to a pointer when you try to use it as one.

like image 89
Dolda2000 Avatar answered Oct 07 '22 01:10

Dolda2000