2

I found a rather strange and unfamiliar usage of the static keyword in C99 and could not find in the language specs nor by experimentation what does it mean:

void set(int t[static 10], int size)
{
    // do something with t, not important here
    for (int i = 0 ; i < size ; ++i) {
        t[i] = size + i;
    }
}

It compiles and run fine (demo on repl.it)

My question is then: what does the static keyword mean in C99 when applied to an array size?


Note: the following is illegal:

int t5[static 10];

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

YSC
  • 38,212
  • 9
  • 96
  • 149
  • 1
    Never mind, I found a perfect duplicate: https://stackoverflow.com/questions/3430315/what-is-the-purpose-of-static-keyword-in-array-parameter-of-function-like-char – YSC Sep 27 '17 at 11:29
  • [This array declaration reference](http://en.cppreference.com/w/c/language/array) should be helpful. – Some programmer dude Sep 27 '17 at 11:29
  • I search SO for like one hour with all the possible ways to express my question without luck. I write the question: BAM! related question is perfect duplicate. Thanks SO. – YSC Sep 27 '17 at 11:30
  • 1
    It means that the array passed must contain at least 10 elements (and it can't be a NULL pointer). – Lundin Sep 27 '17 at 11:30
  • The duplicate is good but what it doesn't mention is the actual language spec, found in C11 6.7.6.2: – Lundin Sep 27 '17 at 11:32
  • 1
    `In addition to optional type qualifiers and the keyword static, the [ and ] may delimit an expression or *. If they delimit an expression (which specifies the size of an array), the expression shall have an integer type. If the expression is a constant expression, it shall have a value greater than zero. The element type shall not be an incomplete or function type. 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.` – Lundin Sep 27 '17 at 11:33
  • 1
    @Lundin maybe you should add it to the duplicate for completeness. – YSC Sep 27 '17 at 11:33

0 Answers0