Home > database >  How to properly cast NULL according to MISRA?
How to properly cast NULL according to MISRA?

Time:01-13

(char *) NULL or char * msg = NULL triggers MISRA 10.5: The value of an expression should not be cast to an inappropriate essential type.

So what is the correct way to cast NULL to some pointer?

N.B.: I am using MISRA checks in CLion.

CodePudding user response:

Assuming you didn't define a home-made version of NULL (which would be another MISRA violation), then char* msg = NULL; is fine and MISRA compliant. Conversion from a null pointer constant to a pointer type without casting is explicitly listed as compliant at multiple places in the guidelines.

(char *) NULL looks like nonsense, what's that supposed to achieve?

Either way, rule 10.5 has nothing to do with any of this since that rule is about value conversions, not about pointer conversions. So it would seem that your MISRA checker is broken, like they tend to be.

  • Related