Utkarsh
Utkarsh

Reputation: 31

What changes should I do in the function-like macro to make it MISRA compliant?

The function-like macro is

#define ARRAYSIZE(_Array)  ((sizeof(_Array)) / (sizeof(_Array[0])))

The error shown is:

Error[Pm154]: in the definition of a function-like macro, each instance of a parameter shall be enclosed in parenthesis (MISRA C 2004 rule 19.10)

Upvotes: 3

Views: 237

Answers (1)

Lundin
Lundin

Reputation: 215350

It's just pedantically saying that you should be doing this sizeof( (_Array)[0] ). Postfix operators have very high precedence so it's unlikely to become an actual problem.

Pedantically you should also use 0u since the intention is for the 0 to correspond to an unsigned type (size_t).

Also please note that leading underscore followed by upper-case letter is reserved for any use in the C language, MISRA or no MISRA.

I'd replace this whole macro with:

#define ARRAY_SIZE(array) ( sizeof(array) / sizeof *(array) )

Should be compliant both to the C language and MISRA-C.

Upvotes: 6

Related Questions