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 1:
void bar(int myArray[static 10]);
This tells the compiler that it should assume that the array passed to bar
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 2
- 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 3:
- Passing
NULL
:
(NULL); bar
warning: null passed to a callee which requires a non-null argument [-Wnonnull]
bar(NULL);
^ ~~~~
- Passing a smaller array:
int a[9];
(a); bar
warning: array argument is too small; contains 9 elements, callee requires at least 10 [-Warray-bounds]
bar(a);
^ ~
- Passing a larger array
int b[11];
(b); bar
[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.