Search code examples
cpointerscompiler-warningssuppress-warningskeil

Pointer parameter was set but never used warning


I have the following function:

void foo(char *ptr_1)
{
    char *ptr_2;

    bar(ptr_2);

    ptr_1 = ptr_2;
}

And get this warning:

parameter "ptr_1" was set but never used

I understand that the warning is technically true, but is irrelevant at the same time. I could suppress it with:

(void)(ptr_1)

But is there a better way?


Solution

  • It is not an irrelevant warning because the assignment has no effect. You could completely remove ptr_1 from the code without changing its behaviour. Your code is equivalent to this:

    void foo(char *)
    {
      char *ptr_2;
      bar(ptr_2);
    }
    

    In other words, the function parameter isn't used for anything. If your intention was to change the pointer at the caller side, you need to either pass a pointer to a pointer and de-reference it, or return the new value and let the caller use its value:

    void foo(char **ptr_1)
    {
        char *ptr_2;
        bar(ptr_2);
        *ptr_1 = ptr_2;
    }
    

    or

    char* foo()
    {
        char *ptr_2;
        bar(ptr_2);
        return ptr_2;
    }