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... 8 6711
Josh Lessard wrote: 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.)
--
Best regards,
Andrey Tarasevich
Josh Lessard wrote: 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.
--
++acr@,ka"
On Sat, 6 Mar 2004 02:14:54 +0000 (UTC), Sam Dennis
<sa*@malfunctio n.screaming.net > wrote in comp.lang.c++: Josh Lessard wrote: 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?
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++ http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jack Klein wrote: On Sat, 6 Mar 2004 02:14:54 +0000 (UTC), Sam Dennis <sa*@malfuncti on.screaming.ne t> wrote in comp.lang.c++: [C99 feature]
But there is no version of C++ that does, so why post it here?
My mistake; I read both groups and got confused.
--
++acr@,ka"
"Josh Lessard" <jr******@plg2. math.uwaterloo. ca> wrote in message
news:Pi******** *************** *************@p lg2.math.uwater loo.ca... 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.
On Sat, 6 Mar 2004, Nick Hounsome wrote: "Josh Lessard" <jr******@plg2. math.uwaterloo. ca> wrote in message news:Pi******** *************** *************@p lg2.math.uwater loo.ca... 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.
On Sat, 6 Mar 2004, Josh Lessard wrote: 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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: David |
last post by:
Some developers in my group are using UNIONS to define their data
types in a C++ program for an embedded system. Are there any pro and
cons in doing this when you can define a CLASS to do the same thing?
I guess there might be some additional overhead with CLASSES, but is
that really an issue with today's computers?
|
by: Mantorok Redgormor |
last post by:
Is this legal?
int foo = { 0 };
gcc gives:
foo.c: In function `main':
foo.c:5: warning: missing braces around initializer
foo.c:5: warning: (near initialization for `foo')
foo.c:5: warning: unused variable `foo'
|
by: Tim Cambrant |
last post by:
Hi. I was reading up a bit on the features of C I seldom use, and I came
across unions. I understand the concept, and that all the contained
variables etc. share the same memory. Thus, when a new value is declared to
a variable in the union, the existing value is overwritten even though the
new value is declared to a different variable than that of the first value.
Now I'm just wondering what the use of this is. I'm sure there are lots,...
|
by: rohit |
last post by:
Hi,
In my couple of years of experience, I have never found a single
instance where I needed to use unions and bitfields(though I have used
structures).I was just imagining where would these find
relevance.Though both of these(bitfields and unions) are used where
space is a constraint(so I can assume always in embedded systems,where
memory is particularly less)and we want to save space/memory.
As far as I have read, there is no...
|
by: qwerty2_reverse_iterator |
last post by:
Is this a bug with the ms compiler (V7.1)? (It seems so at least.)
I get errors when I don't initialize all the const pointer fields of an
anonymous union in a struct. Example:
//T2.h
typedef int Type1;
typedef float Type2;
struct S1
{
| |
by: uralmutlu |
last post by:
Hi,
I was wandering if I can have classes in unions?
I basically have source code in a format very similar to:
union example {
ClassA variable1;
ClassB variable2;
};
|
by: bluejack |
last post by:
A recent post asking for help with unions reminded me of this
component of the C language that I have only used a couple of times,
and those almost entirely out of personal whim -- Unions for the sake
of Unions simply because I wanted to see one in action.
Granted: it makes it possible to save a few bytes of storage when you
have something that can be a chicken or a rooster, but not both, and
you're always going to know which it is.
...
|
by: WaterWalk |
last post by:
Hello. When I consult the ISO C++ standard, I notice that in
paragraph 3.6.2.1, the standard states:
"Objects with static storage duration shall be zero-initialized before
any other initialization takes place."
Does this mean all non-local objects will be zero-initialized before
they are initialized by their initializers(if they have)? For example:
int g_var = 3;
int main() {}
|
by: pereges |
last post by:
Hello, can some one please guide me a little into using unions. I read
about unions in K & R but I am finding it difficult to apply to my
problem at hand. I want to save up some space by using unions . My
questions are :
1. Is it dangerous to use unions ? Is it worth the trouble if I want
to save memory ? Are they error prone ?
2. I read that it is not possible to access more than 1 member at any
instant from a union. What does this...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |