Ahmed Rifaat
Ahmed Rifaat

Reputation: 15

Invalid type conversion while using ANSI c

I am facing this problem as when I am trying to build the code using ANSI C, as I was practicing writing in it and dealing with its rules, it tells me invalid type conversion and I don't know what to do. this is the code line that makes the error, it is a pointer to function:

((CanIf_FuncTypeCanSpecial)(entry->CanIfUserRxIndication))(
    entry->CanIfCanRxPduHrhRef->CanIfCanControllerHrhIdRef,
    entry->CanIfCanRxPduId,
    CanSduPtr,
    CanDlc,
    CanId);

and this is howentry->CanIfUserRxIndication is declared, as void *CanIfUserRxIndication; and this is how CanIf_FuncTypeCanSpecial is declared, as

typedef void (*CanIf_FuncTypeCanSpecial)
                 (uint8 channel, PduIdType pduId, const uint8 *sduPtr, uint8 dlc, Can_IdType canId);

every parameter in the conversion type is the same type as the input parameters except the first one entry->CanIfCanRxPduHrhRef->CanIfCanControllerHrhIdRef it is from type enum not uint8.

You can find the code on GitHub.

and also the MISRA Rule is telling me this:

#1398-D (MISRA-C:2004 11.1/R) Conversions shall not be performed between a pointer to a function and any type other than an integral type

I tried to convert from enum to uint8 to make all of the parameters as what the type conversion CanIf_FuncTypeCanSpecial takes, but nothing happened.

Upvotes: 0

Views: 118

Answers (1)

Jason Hughes
Jason Hughes

Reputation: 3617

If I understand correctly, you are trying to cast an existing function to match a function pointer declaration that has a differing argument type. You can cast the parameters and call such a function, but because function pointers themselves may be used anywhere in the program, at the places where they would be used the code would not know what to cast (which may result in a size difference) this is illegal.

Upvotes: 0

Related Questions