One of the interview questions asked me to “write the prototype for a C function that takes an array of exactly 16 integers” and I was wondering what it could be? Maybe a function declaration like this:
void foo(int a[], int len);
Or something else?
And what about if the language was C++ instead?
In C, this requires a pointer to an array of 16 integers:
It would be called with:
In C++, you can use a reference to an array, too, as shown in Nawaz‘s answer. (The question asks for C in the title, and originally only mentioned C++ in the tags.)
Any version that uses some variant of:
ends up being equivalent to:
which will accept any size of array, in practice.
The question is asked – does
special_case()really prevent a different size of array from being passed. The answer is ‘Yes’.The compiler (GCC 4.5.2 on MacOS X 10.6.6, as it happens) complains (warns):
Change to GCC 4.2.1 – as provided by Apple – and the warning is:
The warning in 4.5.2 is better, but the substance is the same.