Is this code correct?
void foo ( int* p )
{
if ( int* p2 = p ) // single "="
{
*p2++;
}
}
I always been thinking it’s not, but recently i’ve saw such code in a colleague’s of mine sources.
What if “p” is NULL? MS VS 2008 works correct but shows “warning C4706: assignment within conditional expression”.
Thank you.
By raising warning C4706, the compiler is simply questioning whether you actually meant to write
if ( int* p2 == p )instead ofif ( int* p2 = p )as shown.Per 6.4 of the 2003 C++ Standard,
if ( int* p2 = p )is legal:If
pisNULL, condition (int* p2 = p) fails because the value ofp2is 0, and thus implicitlyfalse, and*p2is not incremented.