25

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?

hippietrail
  • 15,848
  • 18
  • 99
  • 158
user2023370
  • 10,488
  • 6
  • 50
  • 83
  • Now I've found this similar question: [http://stackoverflow.com/questions/2374875/should-i-declare-the-expected-size-of-an-array-passed-as-function-argument] It's mainly an iffy comment, though c99 has some additions. – user2023370 Mar 03 '11 at 22:22
  • When you pass an array, it decays to a pointer pointing to first element to the array. So, definitely size of the array should also be a function parameter receiving the array. – Mahesh Mar 03 '11 at 22:22
  • 2
    According to Dennis Ritchie, that syntax is an artifact of trying to ease migration from B (the predecessor of C) to C, long long ago. It's best to pretend that the syntax is not allowed, and to never use it. – Jim Balter Mar 04 '11 at 01:14
  • Here's a little joke: template void bar(int iz[N]) { iz[N+1] = 44; } – user2023370 Mar 04 '11 at 09:24

4 Answers4

23

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)
Community
  • 1
  • 1
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • 4
    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. – user2023370 Mar 03 '11 at 22:17
  • 4
    @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. – nmichaels Mar 04 '11 at 14:07
  • 2
    "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). – brian_o Oct 06 '15 at 21:58
  • 1
    @Jerry Coffin How would you go about passing a multidimensional array and be able to use it as one? – Enok82 Jun 12 '17 at 14:57
19

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.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
7

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.

nmichaels
  • 49,466
  • 12
  • 107
  • 135
0

It is a useful comment when you want to tell to client code that it must pass an array of defined size, i.e:

void foo(const char bar[5]);
/* It is expected that foo function receives an array of size 5 */

Yet, documentation doesn't replace in code checks:

void foo(const char bar[5])
{
    if (!bar) error();
    if (strlen(bar) != 4) error();
    /* ... */
}
Felipe Lavratti
  • 2,887
  • 16
  • 34