Article View: comp.std.c
Article #5581Re: Question on declaration semantics
From: Doug Gwyn
Date: Sat, 14 Jan 1989 15:30
Date: Sat, 14 Jan 1989 15:30
40 lines
2146 bytes
2146 bytes
In article <16490010@hpcllca.HP.COM> curtw@hpcllca.HP.COM (Curt Wohlgemuth) writes:
>......... (file scope)
>int a[5];
>int a[] = {1, 2, 3};
>Are these declarations an invalid combination?
No, the declaration with initializer constitutes the external definition
(the earlier one without initializer was a tentative definition).
The array has five elements, as specified by the first declaration
(the last two elements are initially 0, since they were not explicitly
initialized).
>......... (file scope)
>int a[] = {1, 2, 3};
>int a[5];
This is illegal. After the first declaration, the array has three
elements, and the second declaration contradicts this. The first
declaration constitutes an external definition of a complete type.
(Although at the "[]" the type is incomplete, at the end of the
initializer list the type becomes complete.)
In contrast, consider
int a[];
int a[5] = {1, 2, 3};
which produces an array with five elements.
>As you can probably tell, I'm interested in just where the size of the
>empty-bracketed "a" is to be "filled in". Before the '=', or at the end
>of the initializer?
I don't understand how you're thinking about this. The identifier
either has complete type or not; once an initializer list has been
processed the type is complete (the size is fixed at that point).
The initializer for an incomplete array type sets the size according
to the number of initializing elements provided. The initializer
for an already complete but tentatively defined type (i.e. one
without initializer in the earlier declaration) can contain any
number of elements not to exceed the already declared size.
Elements not explicitly initialized start out with 0 value.
Technical Details
Message-ID:
<9380@smoke.BRL.MIL>Path:
arc1-ca.pugleaf.net!utzoo!attcan!uunet!lll-winken!lll-lcc!ames!haven!adm!smoke!gwynReferences:
<16490010@hpcllca.HP.COM>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!attcan!uunet!lll-winken!lll-lcc!ames!haven!adm!smoke!gwyn From: gwyn@smoke.BRL.MIL (Doug Gwyn ) Newsgroups: comp.std.c Subject: Re: Question on declaration semantics Message-ID: <9380@smoke.BRL.MIL> Date: 14 Jan 89 15:30:29 GMT References: <16490010@hpcllca.HP.COM> Reply-To: gwyn@brl.arpa (Doug Gwyn (VLD/VMB) <gwyn>) Organization: Ballistic Research Lab (BRL), APG, MD. Lines: 40