Imtiaz Kabir
Imtiaz Kabir

Reputation: 139

How to properly cast NULL according to MISRA?

(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.

Upvotes: 0

Views: 186

Answers (1)

Lundin
Lundin

Reputation: 214780

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.

Upvotes: 2

Related Questions