Search code examples
cdereferencebracketsdead-codecppcheck

Difference between *f and (*f) in C?


CppCheck (v1.72) says there is a difference when using (*f) or just *f. The this case

void test(float *f)
{
    float a = 0.0f;
    if(*f>a)
    {
        (*f) += 0.01f;
        if(*f<a) *f=a;
    }
}

cppCheck says "Opposite conditions in nested 'if' blocks lead to a dead code block, where as

void test(float *f)
{
    float a = 0.0f;
    if(*f>a)
    {
        *f += 0.01f;
        if(*f<a) *f=a;
    }
}

makes cppCheck happy. What exactly is the difference?


Solution

  • There's no difference. Ideally, cppcheck should be warning you in both cases, because your code isn't logical.

    However, cppcheck isn't infallible. Don't assume that if cppcheck shows no issues, there aren't any. It's just another useful tool that helps spot bad code that might otherwise go undetected.