Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
314 views
in Technique[技术] by (71.8m points)

Misra Violation 10.1 regarding statement operations (MISRA C 2012)

i need some help with a piece of code. I have an array, lets call it array[4]. Now I want to check that at least 3 elements of this array are taller than a threshold. (if statement)

E.g.

if(2 > ((array[0] > threshold) + (array[1] > threshold) + (array[2] > threshold) + (array[3] > threshold) ))

Here Misra is complaining. (Rule 10.1 Unpermitted operand operator "+") Is there another way to code this if statement without checking every possible permutation ?

Cheers


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

How about explicitly converting those pesky non-addable booleans to integer 1/0?:

if(2 > ((array[0] > threshold?1:0) + (array[1] > threshold?1:0) + (array[2] > threshold?1:0) + (array[3] > threshold?1:0) ))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...