Pointer parameter was set but never used warning

5.5k views Asked by At

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?

3

There are 3 answers

1
juanchopanza On BEST ANSWER

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;
}
2
Sourav Ghosh On

I understand that the warning is technically true, but is irrelevant at the same time.

Well, so is the code here. From this point of view of your code, you can get rid of the offending instruction itself.

Also (my personal opinion), it is almost always better to deal with the warnings rather that suppressing them. They are there for a reason ,after all.

0
Sudarshan Patil On

Simple way is....

int dClock;//variable initiated without used anywhere.
dClock = dClock;//assign variable to itself to suppress not used warning.

worked in keil!