when do we need to pass the size of array as a parameter

后端 未结 8 2105
青春惊慌失措
青春惊慌失措 2020-12-01 19:00

I am a little bit confused about pass an array in C/C++. I saw some cases in which the signature is like this

void f(int arr[])

some is lik

8条回答
  •  长情又很酷
    2020-12-01 19:38

    First, an array passed to a function actually passes a pointer to the first element of the array, e.g., if you have

    int a[] = { 1, 2, 3 };
    f(a);
    

    Then, f() gets &a[0] passed to it. So, when writing your function prototypes, the following are equivalent:

    void f(int arr[]);
    void f(int *arr);
    

    This means that the size of the array is lost, and f(), in general, can't determine the size. (This is the reason I prefer void f(int *arr) form over void f(int arr[]).)

    There are two cases where f() doesn't need the information, and in those two cases, it is OK to not have an extra parameter to it.

    First, there is some special, agreed value in arr that both the caller and f() take to mean "the end". For example, one can agree that a value 0 means "Done".

    Then one could write:

    int a[] = { 1, 2, 3, 0 }; /* make sure there is a 0 at the end */
    int result = f(a);
    

    and define f() something like:

    int f(int *a)
    {
        size_t i;
        int result = 0;
        for (i=0; a[i]; ++i)  /* loop until we see a 0 */
            result += a[i];
        return result;
    }
    

    Obviously, the above scheme works only if both the caller and the callee agree to a convention, and follow it. An example is strlen() function in the C library. It calculates the length of a string by finding a 0. If you pass it something that doesn't have a 0 at the end, all bets are off, and you are in the undefined behavior territory.

    The second case is when you don't really have an array. In this case, f() takes a pointer to an object (int in your example). So:

    int change_me = 10;
    f(&change_me);
    printf("%d\n", change_me);
    

    with

    void f(int *a)
    {
        *a = 42;
    }
    

    is fine: f() is not operating on an array anyway.

提交回复
热议问题