S
S.Tobias
[Newsgroups restricted to c.l.c. only]
The question was about initialization of a (deeply) nested member.
I don't understand what you're trying to say. Everything is
about values, not bytes or representations.
Initializers of aggregates or unions always initialize (recursively and
in order) their members, not "union as a whole". The representation
of a union is that of the last stored to member, plus some unspecified
bytes of other members and padding.
Initializer for a union always initializes the first member.
Maybe. Maybe not.
In comp.lang.c glen herrmannsfeldt said:Marcel van Kervinck wrote:Dear all,
I would like to confirm my suspicion of a compiler
bug in gcc4 for MacOSX. The code example below expects
that the initializer of 'v' sets all elements in
v.u.bytes[] to zero, as specified by the C99 standard:(snip)typedef struct {
union {
unsigned char bytes[2];
} u;
} vector_t;
It seems that none of the posts so far have mentioned the union.
The question was about initialization of a (deeply) nested member.
As I understand, though I could be wrong, the standard sets the
value of the variable to zero, not just bytes. (The distinction
is important when using memset()).
I don't understand what you're trying to say. Everything is
about values, not bytes or representations.
As some variable types might have non-zero bit representation when
the value is zero, and a union could have more than one type of
variable, it seems slightly possible that the restriction is lifted
for unions.
Initializers of aggregates or unions always initialize (recursively and
in order) their members, not "union as a whole". The representation
of a union is that of the last stored to member, plus some unspecified
bytes of other members and padding.
I might even wonder about using initializers when you
can't specify which of the union variables is being initialized.
Initializer for a union always initializes the first member.
Still, on most machines float, double, and pointers should initialize
with all bits zero, and most systems do just that.
Maybe. Maybe not.
Consider:
typedef struct {
union {
double d;
unsigned char bytes[2];
} u;
} vector_t;
v.u.d is initialized with `1'.int main(void)
{
int i;
for (i=0; i<2; i++) {
vector_t v = {{{1}}};
This is unspecified, even if we know the representation of the `double' type.assert(v.u.bytes[0] == 1);
v.u.bytes[1]++;
}
}