Domanda

I have a question about the following code:

#define NUM_DAYS 60
#define NUM_PEOPLE 30

int days[NUM_DAYS];
int people[NUM_PEOPLE];

int size;

size = sizeof(day) > sizeof(people) ? sizeof(day) : sizeof(people);

while the macros NUM_PEOPLE can be changed to bigger than NUM_DAYS or less than NUM_DAYS, then I got the warning: Warning 506: Constant value Boolean, how to fix it? or are there other ways to walk around it except change constant to variable?

È stato utile?

Soluzione 2

Your checker is informing you that sizeof(day) and sizeof(people) are known at compile time, and so the conditional will always take one branch, and never the other.

As an alternative to suppressing the warning on your tool, you can modify your code to use a conditional preprocessor directive to make the taken branch explicit.

#if (NUM_DAYS > NUM_PEOPLE)
#define SIZE sizeof(day)
#else
#define SIZE sizeof(people)
#endif

size = SIZE;

Altri suggerimenti

It's a harmless warning by PC-Lint.

A sizeof expression is an integer constant (well except when its operand is a variable length array, which is not your case) and PC-Lint just notifies you that:

sizeof(day) > sizeof(people)

is a boolean constant (value 1 here) in your program.

You can get rid of the warning by adding the following comment right after the relational expression:

 size = sizeof(day) > sizeof(people) /*lint -save -e506 */ ? sizeof(day)
        : sizeof(people);

Be sure to include the comment right after the > expression otherwise the warning will not disappear.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top