0

Given,

int last(int arr[3][6]) {
    return arr[2][5];
}

I want to make it clear that arr can't be null with the static keyword.
The problem is that the following code won't work.

int last(int arr[static 3][static 6]) {
    return arr[2][5];
}

Using gcc13 -std=c2x I get:

error: static or type qualifiers in non-parameter array declarator

Why can I do arr[static 3][6] and not arr[static 3][static 6] ?

2
  • All dimensions except the left-most need to be defined. The dimensions can be defined statically, e.g. [6], or can be defined variably e.g. [cols] assuming the expression cols can be evaluated and the implementation supports pointers to variable length arrays.
    – Ian Abbott
    Commented Jul 8 at 17:58
  • In this context, static means "at least". The compiler cannot calculate address of arr[1] if it doesn't know how large arr[0] is. Something like "at least xy bytes" is not useful here.
    – Gerhardh
    Commented Jul 9 at 9:32

1 Answer 1

5

In a parameter declared as an array of arrays, the C standard defines static only in the first dimension, and that is the only dimension you need it in.

C 2018 6.7.6.2 1 says:

… The optional type qualifiers and the keyword static shall appear only in a declaration of a function parameter with an array type, and then only in the outermost array type derivation.

static is useful because the outermost dimension otherwise would be lost. The parameter declaration int a[3][6] is automatically adjusted to int (*a)[6], which is a pointer to an array of 6 int, but there is no information about how many arrays of 6 int are at that location.

int a[static 3][6] is also adjusted to int (*a)[6] but with the additional information that there should be at least 3 arrays of 6 int at that location.

There is no need for static on the second dimension because the information about the second dimension is not lost. int [6] is a complete type; it is an array of 6 int. If we have a pointer to one array of 6 int, there are 6 int there. If we have a pointer to three arrays of 6 int, there are 3 arrays of 6 int there (18 int total).

3
  • Thank you, this is exactly what I was looking for. Follow-up question: In your reply you wrote should be at least 3 arrays of 6 int at that location , is this really "at least 3" or you meant "exactly 3" ?
    – gberth
    Commented Jul 8 at 18:07
  • @gberth: It is “at least.” C 2018 6.7.6.3 7 says “… If the keyword static also appears within the [ and ] of the array type derivation, then for each call to the function, the value of the corresponding actual argument shall provide access to the first element of an array with at least as many elements as specified by the size expression.” Commented Jul 8 at 18:21
  • Interesting; this is a nuance that I have missed. I wonder how clang and GCC handle it when it comes to static analysis. Thanks again Eric!
    – gberth
    Commented Jul 8 at 18:58

Not the answer you're looking for? Browse other questions tagged or ask your own question.