Can "long long [(,)]" declaration somehow work in C?


I am preparing for our programming test and I read this long long A[(10,10)] declaration (it was in some previous test in our course), which I have no more information about. Only other thing I know about it, is that it is not possible to initialize variable declared this way by calling A[5][1]=something. Otherwise, I would assume it is some kind of 2D array.
It also could be comma operator but the gcc compiler actually doesn't recognise it.

abc.c:3:16: error: expected ']'
long long A[10,10];
^ abc.c:3:13: note: to match this '['
long long A[10,10];

Do you have any clue if it is a thing, or just some nonsense? (I was trying to Google it, but these things aren't that easy to find...)
Thank you.

Show source
| C   | declaration   2017-01-07 20:01 2 Answers

Answers ( 2 )

  1. 2017-01-07 20:01

    In array declarations, a constant-expression is expected, which is a subset of expression. Specifically, the comma operator and assignment expressions are not part of the set.

    Array declarators are a kind of direct-declarator:

    direct-declarator: ... |
        direct-declarator "[" constant-expression? "]";
    constant-expression: conditional-expression;
    expression: assignment-expression | expression "," assignment-expression;
    assignment-expression: conditional-expression |
        unary-expression assignment-operator assignment-expression;

    So the grammar doesn't allow for a comma here.

  2. 2017-01-07 21:01

    To answer your question "Do you have any clue if it is a thing, or just some nonsense?": Any declaration which is that non-intuitive that experienced programmers have to consult cpp reference is IMHO clearly nonsense.

    I tested expression long long A[(10,10)] with Apple LLVM 8.0 compiler and C99 language dialect, and it worked. When consulting the cpp reference concerning comma operator, one can find the following:

    Top-level comma operator is also disallowed in array bounds

    // int a[2,3]; // error

    int a[(2,3)]; // OK, VLA array of size 3 (VLA because (2,3) is not a constant expression)

    So long long A[(10,10)] seams to be equivalent to long long A[10], where the 10 is the second part of non top-level comma expression (10,10). Interesting thing may be that an array declared this way is treated as VLA (variable length array, which's size is determined at runtime).

◀ Go back