9

I am reading C Programming: A Modern Approach by K.N.King to learn the C programing language and the current chapter tells about functions, and also array parameters. It is explained that one can use constructs like these to express the length of array parameters:

1.

void myfunc(int a, int b, int[a], int[b], int[*]); /* prototype */

void myfunc(int a, int b, int n[a], int m[b], int c[a+b+other_func()]) {
... /* body */
}

2.

void myfunc(int[static 5]); /* prototype */

void myfunc(int a[static 5]) {
... /* body */
}

So the question(s) are:

a. Are the constructs in example 1 purely cosmetic or do they have an effect on the compiler?

b. Is the static modifier in this context only of cosmetic nature? what exactly does it mean and do?

c. Is it also possible to declare an array parameter like this; and is it as cosmetic as example 1 is?

void myfunc(int[4]);

void myfunc(int a[4]) { ... }
3
  • 2
    Nobody knows. Do experiments :) Commented Apr 27, 2015 at 12:09
  • 4
    @i486 Please stop lying. There is a standard for the C language. Of course somebody knows. For reference, doing experiments with C is a bad idea because every compiler does things a little different. Refer to the standard to be sure. Commented Apr 27, 2015 at 12:12
  • It seems that all this gets erased away on compiling and no additional checks are done by the compiler. The expressions inside the brackets are evaluated at runtime but their value seems to be lost. But is that all there is to say about that? And what exactly does static cause? Commented Apr 27, 2015 at 12:12

1 Answer 1

2

The innermost dimension of function array parameters is always rewritten to a pointer, so the values that you give there don't have much importance, unfortunately. This changes for multidimensional arrays: starting from the second dimension these are then used by the compiler to compute things like A[i][j].

The static in that context means that a caller has to provide at least as many elements. Most compilers ignore the value itself. Some recent compilers deduce from it that a null pointer is not allowed as an argument and warn you accordingly, if possible.

Also observe that the prototype may have * so clearly the value isn't important there. In case of multidimensional arrays the concrete value is the one computed with the expression for the definition.

Sign up to request clarification or add additional context in comments.

5 Comments

Maybe sizeof a will give different results according to definition.
@i486, what do you mean? sizeof can only be used inside the definition of the function. `sizeof`` of a pointer parameter will always return the size of that pointer, not the array to which it points.
I mean that sizeof may be different according to array size in function definition. All other can be the same - e.g. between func( int a[5] ) and func( int a[8] ). You define a parameter as array - in fact it is used as pointer. But sizeof a will be calculated accoring to array size in definition. (Not 100% sure and not tested, only guess.)
@i486, no, pointers are still pointers and sizeof a pointer will never tell you the size of the array that it refers to.
Your first paragraph is incorrect. The innermost dimension of array parameter is always rewritten to a pointer and this doesn't change for multidimensional arrays. Also at the same paragraph this statement - "starting from the second dimension these are then used by the compiler to compute things like A[i][j]" is a bit unclear (at least for me).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.