سؤال

I am trying to check whether this equal to zero as

//Address already malloc

void MyCheck (void *Address){

    if ( (long)Address & (~(sizeof(long)-1)) != 0 ){
           printf("Invalid");
    }

}

When try to compile it give me:

Error:  suggest parentheses around comparison in operand 
         of '&' [-Werror=parentheses]
هل كانت مفيدة؟

المحلول

!= has higher precedence than & , so your code is really doing the same as :

if ((long)Address &  ( (~(sizeof(long)-1)) != 0 ) )
                     ^                          ^

gcc suggests this might be an error, which it probably is. You'd want:

if ( ( (long)Address &  (~(sizeof(long)-1)) ) != 0 )
     ^                                      ^

نصائح أخرى

Place a braces around (long)Address & (~(sizeof(long)-1)) in if statement.

if ( ( (long)Address & (~(sizeof(long)-1)) ) != 0 ){

!= is of a higher level of precedence than &, so what you're actually saying is:

if ((long)Address & ((~(sizeof(long)-1)) != 0)){
       printf("Invalid");
}

Instead, put parentheses around the first part:

if (((long)Address & (~(sizeof(long)-1))) != 0){
       printf("Invalid");
}

However, having a lot of background in TI-Basic and C, I would personally leave out the != 0 part:

if ((long)Address & (~(sizeof(long)-1))){
       printf("Invalid");
}

Although other answers have addressed the compiler error, you could sidestep the problem with:

if ((uintptr_t) Address % sizeof(long)) …

(To get uintptr_t, include <stdint.h>. And, if you are using C 2011, you might want to replace sizeof(long) with _Alignof(long).)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top