This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

typedef union problem

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?

Parents
  • Hi Per,
    thanks for your reply. My first idea was to use void pointers. But finally I decided to reduce the risk a little bit.

    I placed enough warnings in my code, that this corner should not be touched without switching on the brain before.

    This is the definition of my pointer-array

    void (*azfSet[IOACC_ATT_ANZ][5])(word, USR_tu_Parameter)
    

    I hope this scares enough :-)

    Anyway: it should be possible to pass a numeric value to a function with union as parameter (standard in c++ without any use of unions).

Reply
  • Hi Per,
    thanks for your reply. My first idea was to use void pointers. But finally I decided to reduce the risk a little bit.

    I placed enough warnings in my code, that this corner should not be touched without switching on the brain before.

    This is the definition of my pointer-array

    void (*azfSet[IOACC_ATT_ANZ][5])(word, USR_tu_Parameter)
    

    I hope this scares enough :-)

    Anyway: it should be possible to pass a numeric value to a function with union as parameter (standard in c++ without any use of unions).

Children
  • But finally I decided to reduce the risk a little bit.

    But you didn't. That union is exactly as risky as passing stuff via (void *). Heck, even variable-number-of-arguments functions would be (slightly) safer than that!

    Oh, and the only reason for anyone to be scared of a declaration an array of function pointers like this:

    void (*azfSet[IOACC_ATT_ANZ][5])(word, USR_tu_Parameter)
    


    is that you decided against doing it in a much less confusing way, e.g.

    #define MEANINGFUL_NAME 5
    typedef void (*azfFunction_t)(word, USR_tu_Parameter);
    azfFunction_t azfSet[IOACC_ATT_ANZ][MEANINGFUL_NAME]