Re: Structure initialization
John Bode wrote:
.... snip ...
>
Sort of. Here's the language from n1124:
>
"6.7.8. 21 If there are fewer initializers in a brace-enclosed list
than there are
elements or members of an aggregate, or fewer characters in a string
literal used
to initialize an array of known size than there are elements in the
array, the
remainder of the aggregate shall be initialized implicitly the same
as objects that
have static storage duration."
>
And the section on initialization of static objects:
>
"10 If an object that has automatic storage duration is not
initialized explicitly,
its value is indeterminate. If an object that has static storage
duration is not
initialized explicitly, then:
— if it has pointer type, it is initialized to a null pointer;
— if it has arithmetic type, it is initialized to (positive or
unsigned) zero;
— if it is an aggregate, every member is initialized (recursively)
according to these rules;
— if it is a union, the first named member is initialized
(recursively) according to
these rules."
A little more readable if taken from N869.txt:
[#21] If there are fewer initializers in a brace-enclosed
list than there are elements or members of an aggregate, or
fewer characters in a string literal used to initialize an
array of known size than there are elements in the array,
the remainder of the aggregate shall be initialized
implicitly the same as objects that have static storage
duration.
and
[#10] If an object that has automatic storage duration is
not initialized explicitly, its value is indeterminate. If
an object that has static storage duration is not
initialized explicitly, then:
-- if it has pointer type, it is initialized to a null
pointer;
-- if it has arithmetic type, it is initialized to
(positive or unsigned) zero;
-- if it is an aggregate, every member is initialized
(recursively) according to these rules;
-- if it is a union, the first named member is initialized
(recursively) according to these rules.
See <http://cbfalconer.home .att.net/download/n869_txt.bz2>
--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home .att.net>
Try the download section.
** Posted from http://www.teranews.com **
John Bode wrote:
jb.si...@lmco.c om wrote:
>
>
>Recently I was pinged in a code review about my use of the
>initializati on method
>AStruct myStruct = { 0 } ;
>>
>Which initializes all elements of the myStruct to 0.
>>
>initializati on method
>AStruct myStruct = { 0 } ;
>>
>Which initializes all elements of the myStruct to 0.
>>
>>
>so it seems that the construct ={0}, sets the fisrst element to
>0 and since there are no more initializers present, all other
>structure members are set to zero by default. Is this correct ?
>so it seems that the construct ={0}, sets the fisrst element to
>0 and since there are no more initializers present, all other
>structure members are set to zero by default. Is this correct ?
Sort of. Here's the language from n1124:
>
"6.7.8. 21 If there are fewer initializers in a brace-enclosed list
than there are
elements or members of an aggregate, or fewer characters in a string
literal used
to initialize an array of known size than there are elements in the
array, the
remainder of the aggregate shall be initialized implicitly the same
as objects that
have static storage duration."
>
And the section on initialization of static objects:
>
"10 If an object that has automatic storage duration is not
initialized explicitly,
its value is indeterminate. If an object that has static storage
duration is not
initialized explicitly, then:
— if it has pointer type, it is initialized to a null pointer;
— if it has arithmetic type, it is initialized to (positive or
unsigned) zero;
— if it is an aggregate, every member is initialized (recursively)
according to these rules;
— if it is a union, the first named member is initialized
(recursively) according to
these rules."
[#21] If there are fewer initializers in a brace-enclosed
list than there are elements or members of an aggregate, or
fewer characters in a string literal used to initialize an
array of known size than there are elements in the array,
the remainder of the aggregate shall be initialized
implicitly the same as objects that have static storage
duration.
and
[#10] If an object that has automatic storage duration is
not initialized explicitly, its value is indeterminate. If
an object that has static storage duration is not
initialized explicitly, then:
-- if it has pointer type, it is initialized to a null
pointer;
-- if it has arithmetic type, it is initialized to
(positive or unsigned) zero;
-- if it is an aggregate, every member is initialized
(recursively) according to these rules;
-- if it is a union, the first named member is initialized
(recursively) according to these rules.
See <http://cbfalconer.home .att.net/download/n869_txt.bz2>
--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home .att.net>
Try the download section.
** Posted from http://www.teranews.com **
Comment