Frequently Asked Array Questions

Evan Marshall Manning manning at nntp-server.caltech.edu
Sun Mar 31 04:21:31 AEST 1991


scs at adam.mit.edu (Steve Summit) writes:

>>    Today, I ran across a situation where a peer had:
>>		char array[7];
>>	    if (array == 0)
>>    No compiler we have complains about it.
>>    They complain about a test for equality with any other constant
>>    but 0, but even then, only that it is an illegal pointer/integer 
>>    combination.   (I understand that difference).

>The conditional expression

>	if (array == 0)

>does not match any of the three exceptions, so the compiler
>dutifully generates a pointer to the first element of the array,
>and then compares it to the null pointer, which is, as Eric
>correctly observes, guaranteed not to succeed.  When exposed to
>the light of day like this, this behavior does seem odd, but the
>compiler is just blindly following the rules, as compilers are
>wont to do.

>A legitimate message from the compiler in this case would be
>"warning: constant in conditional context."  (A more interesting
>message might be "warning: comparison of constants.")  Standard
>lint, however, does not make this diagnosis, presumably because
>it does not treat array names as the constants they really are.
>(In this case, the array name is only quasi-constant, because its
>storage class is auto.  However, lint doesn't complain about
>comparisons of statically-allocated array names, either.)

>I've added this case to my lint wish list.

PC-Lint from Gimpel (and presumably Flexi-Lint from the same source)
Has this to say:
                 _
        if (p == 0)
temp2.c  6  Warning 506: Constant value Boolean

Again, I highly recommend this product.

***************************************************************************
Your eyes are weary from staring at the CRT for so | Evan M. Manning
long.  You feel sleepy.  Notice how restful it is  |      is
to watch the cursor blink.  Close your eyes.  The  |manning at gap.cco.caltech.edu
opinions stated above are yours.  You cannot       | manning at mars.jpl.nasa.gov
imagine why you ever felt otherwise.               | gleeper at tybalt.caltech.edu
i=3;do{putchar(((0x18|1<<!(i&2)|(!!i==i)*2)<<2|2>>i%2)^(2<<1+i));}while(i--);



More information about the Comp.lang.c mailing list