Disclaimer: OP tags are rather ambiguous, so this answer uses the code as a coordinate system, which is C ++ (using iostream by pulling the namespace std , cout ).
You are using union inappropriate way. But we will come back to this later.
ei = 20;
Your code first uses a union like i , an integer. This is normal. But what you did later is really not a good idea. First you did two somewhat acceptable things:
cout << &e.j; cout << &e.i;
You requested the address of the two int in the union, which is negligible because they all share storage, and therefore the address of the first byte is split.
cout << &e.c[0]; //Why can't I print this address cout << ec[1]; // Why can't I print this value
Now where do you cross the line. Now you do implicit arithmetic of pointers and dereferencing in terms of indexing to the char[] array, and even if you try to get the address of the first element, there is a possible evaluation of the element that is not the last set to Union. So this is a big no no.
In addition, &e.c[0] is basically a char* that will be "intercepted" by cout and treated as a C-style string. It will not treat it as a simple address.
cout << ec[1];
Undefined. "But, but!" I hear some of you say. Yes, this is UB in C ++. Valid in C99 (6.5 / 7), and only with the help of a footnote and some adhesive tape. This is a simple question already explained by LightnessRacesInSpace and Mystical in the comments on this answer and others.
Yes, you can use any typed variable that you have for the char array, and use it for whatever purpose you have in mind. But type-purging through unions is illegal in C ++, there are no grounds and excuses. Yes, that might work. Yes, if this does not bother you, you can continue to use it. But according to the C ++ standard, this is clearly illegal.
If this member was not the last member of the union to which you assigned a value, you will not receive its value. It is so simple.
Unions in C ++ have the goal described below. They may also have member functions and access specifiers. They cannot have virtual functions or static members. They also cannot be used as a base class or inherit from anything. And they should not be used for customization. This is illegal in C ++.
Read more!
Connection Overview
An association:
- A way to reuse memory.
- What is it.
Merging is not performed:
- How to use a cowboy between elements of the union
- Method of cheating with a strict pseudonym.
Even MSDN understood correctly:
A union is user-defined data or a class type that, at any given time, contains only one object from its list of participants (although this object can be an array or class type).
What does it mean? This means that you can define something like that:
union stuff { int i; double d; float f; } m;
The idea is that they all sit in the same space in memory. Union storage is inferred from the largest data type in this implementation. The platforms here have great freedom. Specification freedom cannot cover. Not C. Not C ++.
You should not write to the union as an int , and then read it as a float (or something else) as a way of some kind of weird reinterpret_cast cowboy.
Using std::cout is, for example, goals and simplicity.
It's illegal:
mi = 5; std::cout << mf; // NO. NO. NO. Please, no.
It is legal:
mi = 5; std::cout << mi;
Please note that there is no "crossfire". This is the intended use. Thin memory storage for three variables, used three different times without a fight.
How did misconception arise? Some very bad people woke up one day, and I'm sure one of them was a 3D programmer and thought about it:
// This is wrong on so many different levels. union { float arr[4]; struct { float x,y,z,w; }; };
He undoubtedly had a “noble idea”, access to the 4-tuple, both to a floating-point array and to individual xyzw members. Now you know why this is wrong from the point of view of the unions, but there is one more failure:
C ++ has no anonymous structures . It has anonymous alliances, for the purposes illustrated above, to get closer to the intended use (by dropping the prefix m. "), Since you can see exactly how beneficial this is to the general idea of alliances.
Do not do this. You are welcome.