I have a problem with typedef union as parameter for a function-call.
definition of the typedef
union uParameter { byte bByte; // 8 bit unsigned int16 iInt16; // 16 bit signed int32 iInt32; // 32 bit signed }; typedef union uParameter USR_tu_Para;
prototype of a function that uses this typedef
void SetSomething(USR_tu_Para para);
The question is: how can I call the SetSomething-function with a constant parameter.
SetSomething(100);
This call creates a compiler error: #167: argument of type "int" is incompatible with parameter of type "USR_tu_Para"
I tried to cast the parameter witch (USR_tu_Para) or (USR_tu_Para.iInt16). But the compiler denied everything I tried.
Any ideas?
Why a union here? The parameter will occupy a full register in any case.
See here how to use transparent unions at the expense of limited portability: infocenter.arm.com/.../armccref_cacifaid.htm
why a union: i agree at this example there is no need to use a union. But in my application it is a little bit more complicated. I use a pointer to access different functions. These functions imlicitely know if they need 8, 16 or 32 bits. I also misapplied that there are signed datatypes in the union too.
The use of unions at this point simplifies the software a lot.
I did not rumble through the benefit of transparent unions. But the crux of the matter will be that the software has to run under Visual C++ too. I don't think transparent unions will be supported there.
Just remember that the stricter data type checking you can use, the quicker you - or the next developer who may pick up this project - will catch goofs in the code.
With a union that doesn't have a field to say what type of info it contains, together with functions that "automatically" know what union member to pick up, you have completely nuked the compilers ability to track used data types. You could just as well switch to ugly typecasted pointers - yes, very ugly but at least a code reader would know he/she is in very, very dangerous terrain.
Does it?
Wouldn't void* do it?
But I think both give huge risks of problems due to data (mis)alignment...
The union as defined above would be guaranteed to be word aligned. I am not recommending the use of unions here, but we want to get the facts straight.
-- Marcus http://www.doulos.com/arm/
View all questions in Keil forum