Why does C disallow an array return type?

前端 未结 3 702
无人共我
无人共我 2021-01-15 20:58

Why is this valid in C

int * foo(int a,int b){
...
}

but this is invalid

int [] foo(int a,int b){
...
}
相关标签:
3条回答
  • 2021-01-15 21:36

    The syntax is somewhat funny.

    int foo(int a, int b) []
    {
        ...
    }
    

    But it's not allowed anyway. See n1256 6.7.5.3 paragraph 1, "Function declarators".

    A function declarator shall not specify a return type that is a function type or an array type.

    You can return pointers to arrays:

    int (*foo(int a, int b)) []; // Weird syntax, n'est-ce pas?
    

    But you might as well just return a pointer instead, since the following are equivalent:

    int array[] = { ... };
    int *x = array, (*y)[] = &array;
    x[0];
    (*y)[0]; // same as above, just with more cumbersome syntax
    

    Typically, if a function needs to return an array of int, you either return the pointer or pass the pointer in. One of the following:

    int *func(int a, int b); // Allocated by func
    void func(int a, int b, int *array); // Allocated by caller
    void func(int a, int b, int **array); // Allocated by func
    

    The "struct-hack" also works for arrays that have a fixed size:

    struct { int arr[50]; } array_struct;
    struct array_struct func(int a, int b);
    

    But this is not recommended unless the array is small.

    Rationale:

    Arrays are often large, and often have a size not known until runtime. Since parameters and return values are passed using the stack and registers (on all ABIs I know of), and the stack has a fixed size, it is somewhat dangerous to pass such a large object around on the stack. Some ABIs also don't gracefully handle large return values, potentially causing extra copies of return values to be generated.

    The following code can also be dangerous:

    void func(...)
    {
        int arr[BIG_NUMBER]; // potential for stack overflow
        int *ptr = alloca(sizeof(int) * BIG_NUMBER); // potential for stack overflow
    }
    
    0 讨论(0)
  • 2021-01-15 21:37

    First, recall that arrays in C are really just syntactic sugar around pointers to blocks of memory. So C isn't restricting the functionality of the language by forcing the use of the former notation (See the example which follows).

    Also, they may have made this choice to prevent early programmers from writing code like this:

    char *itoa(int n){
        char retbuf[25];
        sprintf(retbuf, "%d", n);
        return retbuf;
    }
    

    ref

    Which looks simple enough, but what happens to the memory retbuf points to at the end of the function? Can the calling functions trust the data in the pointer it gets back?

    0 讨论(0)
  • 2021-01-15 21:46

    In C passing arrays by value is not directly supported (even when you write int [] as a parameter is actually interpreted as int *), if I remember correctly it's some sort of artifact of the passage from BCPL to C.

    That being said, you can actually return arrays encapsulating them into structs:

    struct
    {
        int value[20]
    } foo(int a, int b)
    {
        /* ... */
    }
    

    (this trick obviously works also for parameters)

    0 讨论(0)
提交回复
热议问题