Prototype for variable-length arrays

☆樱花仙子☆ 提交于 2019-12-18 03:40:57

问题


I am trying to write a function that takes an array of an variable size in c.

void sort(int s, int e, int arr[*]){
    ...
}

It says that for variable length arrays, it needs to be bounded in the function declaration. What does that mean? I am using xcode 4.0, with the LLVM compiler 2.0.

Thanks for the help.


回答1:


If you're not using the C99 variable length arrays, the usual solution is to pass in a pointer to the first element, along with any indexes you want to use for accessing the elements.

Here's a piece of code that prints out a range of an array, similar to what you're trying to do with your sort.

#include <stdio.h>

static void fn (int *arr, size_t start, size_t end) {
    size_t idx;
    for (idx = start; idx <= end; idx++) {
        printf ("%d ", arr[idx]);
    }
    putchar ('\n');
}

int main (void) {
    int my_array[] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
    fn (my_array, 4, 6);
    return 0;
}

This outputs elements four through six inclusive (zero-based), giving:

5 4 3

A couple of points to note.

  • Using my_array in that function call to fn automatically "decays" the array into a pointer to its first element. This actually happens under most (not all) circumstances when you use arrays, so you don't have to explicitly state &(my_array[0]).

  • C already has a very good sort function built in to the standard library, called qsort. In many cases, that's what you should be using (unless either you have a specific algorithm you want to use for sorting, or you're doing a homework/self-education exercise).




回答2:


As I see that no one answers the real question, here I give mine.

In C99 you have variable length arrays (VLA) that are declare with a length that is evaluated at run time, and not only at compile time as for previous versions of C. But passing arrays to functions is a bit tricky.

A one dimensional array is always just passed as a pointer so

void sort(size_t n, int arr[n]) {

}

is equivalent to

void sort(size_t n, int *arr){

}

Higher dimensions are well passed through to the function

void toto(size_t n, size_t m, int arr[n][m]){

}

is equivalent to

void toto(size_t n, size_t m, int (*arr)[m]){

}

With such a definition in the inside of such a function you can access the elements with expressions as arr[i][j] and the compiler knows how to compute the correct element.

Now comes the syntax that you discovered which is only useful for prototypes that is places where you forward-declare the interface of the function

void toto(size_t, size_t, int arr[*][*]);

so here you may replace the array dimension by * as placeholders. But this is only usefull when you don't have the names of the dimensions at hand, and it is much clearer to use exactly the same version as for the definition.

void toto(size_t n, size_t m, int arr[n][m]);

In general for a consistent use of that it is just important that you have the dimensions first in the the parameter list. Otherwise they would not be known when the compiler parses the declaration of arr.




回答3:


What you want to do it make your argument an int *; pass in the length of the array (which the caller presumably knows, but this routine does not) as a separate argument. You can pass an array as such an argument.




回答4:


The usage of * inside of array brackets for variable-length arrays is limited to prototypes, and serves merely as a placeholder. When the function is later defined, the array's size should be stored in a variable available at either file scope or as one of the parameters. Here's a simple example:

void foo(int, int[*]);
/* asterisk is placeholder */

void foo(int size, int array[size]) {
/* note size of array is specified now */
}


来源:https://stackoverflow.com/questions/7225358/prototype-for-variable-length-arrays

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!