Why should I declare a C array parameter's size in a function header?

19,702

Solution 1

Can anyone enlighten me as to why I should bother to specify the size of a C array argument in a function header? For example:

void foo (const char sz[6]) { sz[42] = 43; }

IMO, you shouldn't. When you try to pass an array to a function, what's really passed is a pointer to the beginning of the array. Since what the function receives will be a pointer, it's better to write it to make that explicit:

void foo(char const *sz)

Then, since it's now clear that the function has been given no clue of the size, add that as a separate parameter:

void foo(char const *sz, size_t size)

Solution 2

The only meaningful reason to do that is for documentation purposes - to tell the future users that functions expect to receive an array of at least that many elements. But even that is a matter of convention - something that you have to agree upon with other users in advance. The language (the compiler) ignores that size anyway. Your function declaration is equivalent to void foo(int iz[]) and to void foo(int *iz).

The only way to make it somewhat meaningful for the compiler is to declare it as

void foo (int iz[static 6])

which acts as a promise to the compiler that the array will have at least 6 elements, meaning that the compiler will be able to optimize that code using that assumption. Moreover, if you really want to adopt the convention mentioned above, it makes more sense to declare array parameter sizes with static specifically, since the language explicitly defines the semantics of this construct.

What you mean by "we get a useful error" is not clear to me. The code

int is[2] = {1,2,3};
is[42] = 42;

does not contain any constraint violations. It produces undefined behavior, but it is not required to produce a diagnostic message during compilation. In other words, no, we don't get any "useful error" from this.

Solution 3

It's a comment. Arrays are demoted to pointers in function parameters. Comments can still be useful however, even if the compiler doesn't read them.

Share:
19,702
user2023370
Author by

user2023370

Updated on June 28, 2022

Comments

  • user2023370
    user2023370 about 2 years

    Can anyone enlighten me as to why I should bother to specify the size of a C array argument in a function header? For example:

    void foo (int iz[6]) { iz[42] = 43; }
    

    With:

    int is[2] = {1,2,3};
    

    we get a useful error. Perhaps it helps with commenting/documentation?

  • user2023370
    user2023370 over 13 years
    Agreed. The comment is less than useful. A developer may modify the code to handle an array of length 9, say, and accidentally leave the 6 in place.
  • nmichaels
    nmichaels over 13 years
    @user643722: If your problem is that a developer could modify the code to make the comment incorrect, then fail to update the comment, then your problem is with every comment ever written. That hardly seems sufficient to convince people to stop writing them.
  • chux - Reinstate Monica
    chux - Reinstate Monica over 9 years
    Is static in void foo (int iz[static 6]) a C99, C11 innovation?
  • mojuba
    mojuba almost 9 years
    Could you clarify what kind of optimizations?
  • brian_o
    brian_o over 8 years
    "You shouldn't." I wholeheartedly agree. To me, a function which starts void foo (int iz[6]) is implicitly communicating (to the next programmer, not the compiler) that it's implementation enforces the size 6 in some way. We had strange problems with some legacy code that was overrunning its buffers. The size in myfunc(char buffer[size]) was deceptive (or maybe non-C-coders shouldn't be messing with old stuff).
  • AnT stands with Russia
    AnT stands with Russia over 8 years
    @mojuba: For example, generate machine instructions that prefetch array data.
  • Enok82
    Enok82 about 7 years
    @Jerry Coffin How would you go about passing a multidimensional array and be able to use it as one?
  • CoffeeTableEspresso
    CoffeeTableEspresso almost 4 years
    @chux-ReinstateMonica that use of static was added in C99