Search code examples
cwhile-loopc99c89

coding style aginst warning check


During clean compile warning, I met following code:

char *strcpy(char *dest, char *src)
{
    unsigned int i;

    while( dest[i] = src[i] )//assignment in condition
        i++;

    return dest;
}

the base function of the code should be OK, but compiler warn that the assignment in condition, does this part of code have any potential risks? if this kind of warning need be cleaned?


Solution

  • All kinds of warnings need to be cleaned.

    This warning was introduced because = and == is often confused by programmers (hello, Pascal!), and your intent may be explicitly stated by adding parentheses around assignment expression:

    if ((x = y)) // no warning