Hello! I have a typedef union and variable:
typedef union int2char{unsigned char high_byte; unsigned char low_byte; unsigned int int_byte; } int2char; int2char dac_value;
When i assign
dac_value.int_byte = 0xAAAA;
i have
dac_value.int_byte = 0xAAAA; dac_value.low_byte = 0xAA; dac_value.high_byte = 0xAA;
but when i assign,example,0xFF05
dac_value .int_byte = 0xFF05; dac_value.low_byte =0xFF; dac_value.high_byte = 0xFF;
same results are obtained when i assign,example,0x1005
dac_value .int_byte = 0x1005; dac_value.low_byte =0x10; dac_value.high_byte = 0x10;
I can not understand where the error and why?? thanks for the help!.
What do you think happens in a union?
What is the address of your low_byte member? What is the address of your high_byte member? What is the address of your int_byte member.
By the way - what is "int byte"?
You have to use a struct to encapsulate your low_byte and high_byte members to have them stored after each other, and not on top of each other.
By the way - how many threads do you think you can find on this forum that describes problems with using unions for type conversion? A union was added to the standard to allow the same memory space to store _either_ of multiple data types. The standard does not guarantee that you can write to one member and read back reasonable data from another member. All that is covered is the start offset of the members.
What do you think happens when you switch from a compiler that stores an int low-byte before high-byte compared to a compiler that stores an int high-byte before low-byte?
Hi, since all elements of a union are placed at the same location in memory, low_byte and high_byte have the same address and therefore the same value www.cplusplus.com/.../
Thank you for your help, I think to again pick up a textbook and look again, realized his mistake, thank you.
since all elements of a union are placed at the same location in memory, low_byte and high_byte have the same address and therefore the same value nope, not in C
Erik
nope, not in C
Have a look at 6.7.2.1 in ISO/IEC 9899:1999.
Bullet 14 says: "The size of a union is sufficient to contain the largest of its members. The value of at most one of the members can be stored in a union object at anytime. A pointer to a union object, suitably converted, points to each of its members (or if a member is a bit-field, then to the unit in which it resides), and vice versa."
Note that a pointer to a union object points to each of its members - that is because all members of a union starts at the start of the union. That also means that the union must match the alignment of the member that has highest alignment requirements.
{ unsigned char high_byte; unsigned char low_byte; unsigned int int_byte; } int2char; I always do this instead and thus "got caught" unsigned short i; unsigned char c[2];
low_byte and high_byte have the same address and therefore the same value
No, they don't. They do have the same address, but strictly spoken, and in the use case at hand, both of them have no value at all. Their values don't exist because they're not the union variant last written to. Non-existent things have no properties --- not even equality.
Note that the above point should not be pushed too far.
There are a specific situation where assigns to union member a explicitly allows reads from other union members.
§6.5.2.3 bullet 5 claims: "One special guarantee is made in order to simplify the use of unions: if a union contains several structures that share a common initial sequence (see below), and if the union object currently contains one of these structures, it is permitted to inspect the common initial part of any of them anywhere that a declaration of the complete type of the union is visible. Two structures share a common initial sequence if corresponding members have compatible types (and, for bit-fields, the same widths) for a sequence of one or more initial members."
Yes indeed the problem I have decided in two ways, should announce:
typedef union int2char{ unsigned int int_byte; unsigned char char_byte[2];}int2char;
} or
typedef union int2char{ unsigned int int_byte; struct char_byte{ unsigned char high_byte; unsigned char low_byte;}char_byte; }int2char; }
Thanks a lot for such an active assistance also sorry for the low level of English, I use a translator. :)