On Thu, 9 Aug 2018, Martin Sebor wrote:
> But for a declaration at file scope and without an initializer,
> GCC warns that the array is assumed to have one element, but
> then gives an error when sizeof is applied to it:
That's how tentative definitions (C17 6.9.2) work. There's an implicit
initializer of { 0 }, but only at the end of the translation unit, so the
type is incomplete until then and sizeof cannot be applied to it.
> even though the array does have a size of 1. (I guess that's
> because of the [] syntax but the warning sure makes the sizeof
> error surprising.) Auto declarations of arrays with no bound
> and with no intializer are rejected with an error:
>
> char a[]; // error: array size missing in ‘a’
Tentative definitions only exist at file scope (and in standard C they
can't have an incomplete type if they have internal linkage). See 6.7#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."
--
Joseph S. Myers
[email protected]