0

In the function below I'm not using the parameter (void **arg). But since it's unused inside the function compiler gives me the error below:

error: unused parameter 'arg' [-Werror=unused-parameter] bool decodeString(pb_istream_t *stream, const pb_field_t *field, void **arg) 

I tried to suppress it by writing void(arg) inside the function without any luck. Can anyone help me with the correct way?

6
  • 4
    Leave out the variable name: bool decodeString(pb_istream_t *stream, const pb_field_t *field, void **arg) becomes bool decodeString(pb_istream_t *stream, const pb_field_t *field, void **) Commented Oct 27, 2021 at 18:15
  • 1
    Side note: I prefer to comment out the name bool decodeString(pb_istream_t *stream, const pb_field_t *field, void ** /*arg*/ ) because the name may hold useful information about how the parameter should be used, should that information become important later. Commented Oct 27, 2021 at 18:16
  • 1
    Does this answer your question? How to suppress "unused parameter" warnings in C? Commented Oct 27, 2021 at 18:17
  • @MadPhysicist only viable in C++. dbush's answer below works in C and C++. Mind you, my C is pretty stale. Might have changed. Commented Oct 27, 2021 at 18:20
  • @user4581301 - I 2nd the 2nd comment. Commented Oct 27, 2021 at 18:21

1 Answer 1

4

Use the parameter in an expression casted to void. Then the parameter is "used".

bool decodeString(pb_istream_t *stream, const pb_field_t *field, void **arg) { (void)arg; ... } 
Sign up to request clarification or add additional context in comments.

8 Comments

This does not work, found similar answer in stackoverflow.com/questions/3599160/… and tried it before. error: variable or field 'arg' declared void void(arg);
Be very careful with doing this as conceptually you have induced a read of the value. Personally I'd comment arg - you don't lose the self-commenting nature of the function and you can always uncomment it if you need it. This is a good idiomatic (especially in C) technique though. Although I do remember reading something about a nasty bug emanating from this, with gcc and optimisations turned up to the maximum. Might have dreamt it though.
@razz, if this doesn't work in C you have a second problem.
you can also int decodeString(void **) { /* ... */ }
@razz That's a separate issue that should be in a separate question.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.