Comments:"A nice, little known C feature: Static array indices in parameter declarations"
URL:http://hamberg.no/erlend/posts/2013-02-18-static-array-indices.html
The people who created C sure loved keeping the number of keywords low, and today I’m going to show you yet another place you can use the static
keyword in C99.
You might have seen function parameter declaration for array parameters that include the size:
void foo(int myArray[10]);
The function will still receive a naked int *
, but the [10]
part can serve as documentation for the people reading the code, saying that the function expects an array of 10 int
s.
But, you can actually also use the keyword static
between the brackets :
void bar(int myArray[static10]);
This tells the compiler that it should assume that the array passed to foobar
has at least 10 elements. (Note that this rules out a NULL
pointer!)
Doing this serves two purposes:
- The compiler could use this information when optimizing the code
- The compiler can warn callers when it sees them calling the function with anything but an array of 10 or more
int
s.
So, let’s see what actually happens when compiling a program with the above definition of bar
when passing the following three arguments to it :
bar(NULL);
warning: null passed to a callee which requires a non-null argument [-Wnonnull]
bar(NULL);
^ ~~~~
int a[9];
bar(a);
warning: array argument is too small; contains 9 elements, callee requires at least 10 [-Warray-bounds]
bar(a);
^ ~
int b[11];
bar(b);
[no output]
This is great for those cases where you actually know the size of the array a function should be passed since it both serves as documentation for people reading the code, and lets the compiler help you catch mistakes.