0

How to typecast enum to union in C

Hello, I am working on the embedded C and I need to typecast enum to union. I tried doing it constant cast but it is showing error.

18th Sep 2020, 6:09 AM
ASHISH JHA
ASHISH JHA - avatar
8 Answers
+ 2
As far as I understand it your external hardware has its outputs at address (pin?) 1, 2, 3 and 4 while your other hardware has it at 0, 1, 2 and 3? If you use an enum the default value is 0, but you can set the first value to 1 to have the next value be 2, then 3, etc. ( or you can explicitly set each enum value ) typedef enum { A = 1, B, C, D } E; Sorry if I didn't understand correctly.
18th Sep 2020, 11:04 AM
Dennis
Dennis - avatar
+ 1
Not sure what you mean. What's wrong with something like typedef enum { A, B, C, D } E; typedef union { E e; } U; int main() { U u = { .e = B }; }
18th Sep 2020, 10:45 AM
Dennis
Dennis - avatar
+ 1
Okay Thanks I didn't no we can explicitly set the value of enum. Thanks
18th Sep 2020, 11:06 AM
ASHISH JHA
ASHISH JHA - avatar
0
Just curious ... What benefit is gained from this, if it was even possible? Aren't they two different things and purpose?
18th Sep 2020, 7:06 AM
Ipang
0
I am having 2 Hardware that needs to be interfaced with microcontroller. So for that i need both
18th Sep 2020, 8:09 AM
ASHISH JHA
ASHISH JHA - avatar
0
Dennis Advise please?
18th Sep 2020, 10:33 AM
Ipang
0
Let me explain in detail. My external hardware has 4 output whose address start with 1 and other hardware has its address start from 0. So for the second hardware I used enum as it gave me values from 0 to 4 but now I cannot use it for 2nd hardware as the address location of each pin needs to be matched. So If i can typecast enum to union then i can define the var as per my need
18th Sep 2020, 10:50 AM
ASHISH JHA
ASHISH JHA - avatar
0
Or if there is any other way to do it
18th Sep 2020, 10:51 AM
ASHISH JHA
ASHISH JHA - avatar