C: Is it legal to subscript an array of incomplete type?

c, extern, incomplete-type

Solution

The following statement

extern char arr[];

is a declaration with external linkage, and says that `arr` has a type of array of `char`, which implies that `arr` can have an incomplete type.

According to "6.7 Declarations" (n1570):

7 If an identifier for an object is declared with no linkage, the type for the object shall be complete by the end of its declarator, or by the end of its init-declarator if it has an initializer; in the case of function parameters (including in prototypes), it is the adjusted type (see 6.7.6.3) that is required to be complete.

And `arr[7]` equals `*(arr + 7)`, and `arr` need to have a type of "pointer to complete object type", and the type of `arr` will be converted from "array of `char`" to "pointer to `char`" in this case.

According to "6.3.2.1 Lvalues, arrays, and function designators" (n1570):

3 Except when it is the operand of the `sizeof` operator, the`_Alignof` operator, or the unary `&` operator, or is a string literal used to initialize an array, an expression that has type ‘‘array of type’’ is converted to an expression with type ‘‘pointer to type’’ that points to the initial element of the array object and is not an lvalue.

Problem

I can't find the relevant bits in the standard, but gcc and clang allow it, so I guess I' wondering if it's a compiler extension or part of the language. Provide a link if you can. This can arise with things such as this: ``` extern char arr[]; func(arr[7]); /*No error.*/ ``` LATE EDIT: I figured I'd better get a clear understanding of this, which I never did although I had moved on, so starting a bounty which I will award to the first person to give me a clear, concise reference(es) in the C89 standard as to why this is allowed. C99 is acceptable if nobody can find the answer in C89, but you need to look in the C89 standard first.

Original source