Initializing unions

J

Josh Lessard

Given a union definition:

union problem_t {
int mask[2];
struct {
int indices[2];
int ops[2];
} comp;
};

Is it possible to initialize the struct containing the two arrays when
declaring a variable of this type? I've tried just about every
combination I can think of, but I can't seem to get it to work. For
example:

int main() {
problem_t problem = { {}, { { 0, 0 }, { 0, 0 } } };
}

union.cc: In function `int main()':
union.cc:12: excess elements in aggregate initializer

I've tried omitting the empty array representing the first element of the
union and that doesn't work either. I can't simply reverse the two union
elements, because I initialize it differently in different parts of my
program depending on how I want the union used.

Any ideas? Thanks in advance...
 
A

Andrey Tarasevich

Josh said:
Given a union definition:

union problem_t {
int mask[2];
struct {
int indices[2];
int ops[2];
} comp;
};

Is it possible to initialize the struct containing the two arrays when
declaring a variable of this type?

No. You'll have to use assignment instead of initialization

(Unless you have another union, which already holds the desired values.
In that case you can copy initialize the new union from the old one.)
 
S

Sam Dennis

Josh said:
union problem_t {
int mask[2];
struct {
int indices[2];
int ops[2];
} comp;
};

Is it possible to initialize the struct containing the two arrays when
declaring a variable of this type?

With a designator, yes:

union problem_t problem = { .comp = { { 0, 0 }, { 0, 0 } } };

If you want your code to be portable to pre-C99 implementations, though,
you'll just have to use an assignment to achieve this.
 
J

Jack Klein

Josh said:
union problem_t {
int mask[2];
struct {
int indices[2];
int ops[2];
} comp;
};

Is it possible to initialize the struct containing the two arrays when
declaring a variable of this type?

With a designator, yes:

union problem_t problem = { .comp = { { 0, 0 }, { 0, 0 } } };

If you want your code to be portable to pre-C99 implementations, though,
you'll just have to use an assignment to achieve this.

There is a version of C, not particularly widely available
unfortunately, that will accept this definition with initialization.

But there is no version of C++ that does, so why post it here?
 
N

Nick Hounsome

Josh Lessard said:
Given a union definition:

union problem_t {
int mask[2];
struct {
int indices[2];
int ops[2];
} comp;
};

Is it possible to initialize the struct containing the two arrays when
declaring a variable of this type? I've tried just about every
combination I can think of, but I can't seem to get it to work. For
example:

int main() {
problem_t problem = { {}, { { 0, 0 }, { 0, 0 } } };
}

union.cc: In function `int main()':
union.cc:12: excess elements in aggregate initializer

I've tried omitting the empty array representing the first element of the
union and that doesn't work either. I can't simply reverse the two union
elements, because I initialize it differently in different parts of my
program depending on how I want the union used.

Any ideas? Thanks in advance...

ref 8.5.1
.....the braces shall only contain an initializer for the first member of the
union.

so reorder your union and treat it like it was just the first member.

Alternatively add one or more constructors.
 
J

Josh Lessard

Josh Lessard said:
Given a union definition:

union problem_t {
int mask[2];
struct {
int indices[2];
int ops[2];
} comp;
};

Is it possible to initialize the struct containing the two arrays when
declaring a variable of this type? I've tried just about every
combination I can think of, but I can't seem to get it to work. For
example:

int main() {
problem_t problem = { {}, { { 0, 0 }, { 0, 0 } } };
}

union.cc: In function `int main()':
union.cc:12: excess elements in aggregate initializer

I've tried omitting the empty array representing the first element of the
union and that doesn't work either. I can't simply reverse the two union
elements, because I initialize it differently in different parts of my
program depending on how I want the union used.

Any ideas? Thanks in advance...

ref 8.5.1
....the braces shall only contain an initializer for the first member of the
union.

so reorder your union and treat it like it was just the first member.

Alternatively add one or more constructors.

Thank you very much for your suggestions, but I've already considered
them. Reordering the union members won't help because the size of the
'mask' array is different for each platform I'm implementing my program on
(the size is actually set by a platform specific const int), and there are
times where I need to fill that entire array.

Constructors also won't work because I'm trying to initialize an array of
these unions. I guess I'll just have to make it a struct, and thus use an
extra four bytes for the two arrays of two elements.

Thanks everyone for your suggestions.
 
J

Josh Lessard

Constructors also won't work because I'm trying to initialize an array of
these unions. I guess I'll just have to make it a struct, and thus use an
extra four bytes for the two arrays of two elements.

Erm...extra 4 *ints* for the two arrays of two elements. Sorry about
that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Initializing std::map Objects 9
unions 2
Alternative to Malloc in C 0
safe "struct hack"? 11
Memory layout in unions 6
URGENT 1
Static initialization of unions containing arrays etc. (casting) 3
Initializers 3

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top