gcc warning - Why does a "function name" evaluate to true in C and how to get warned on it -
i stumbled across following behaviour of gcc 3.2.2 writing c program:
in if statement forgot braces of function , wrote:
if(myfunc)...
instead of if(myfunc())...
this did not generate error neither warning although have pretty every warning turned on.
it evaluated true
. why writing legal code in first place ? because function exists/has address ? know how 1 avoid such mistakes or if there warning option overlooked ? issue better solved in later gcc versions ?
here exact compiler call completeness:
msp430-gcc -g -os -mmcu=msp430x1611 -wall -w -wfloat-equal -wundef -wshadow -wpointer-arith -wbad-function-cast -wcast-qual -wwrite-strings -wsign-compare -waggregate-return -wstrict-prototypes -wmissing-prototypes -wmissing-declarations -wredundant-decls -wnested-externs -wimplicit-function-declaration -werror
(since i'm forced use gcc 3.2.3 there no -wextra)
if (myfunc)
equivalent if (&myfunc)
, you're testing address of function, of course non-zero, i.e. true.
with gcc 4.2.1 , -wall
following warning:
myfunc.c:11: warning: address of ‘myfunc’ evaluate ‘true’
Comments
Post a Comment