5

I was reading some source code, and this came up;

struct Cookie *
Curl_cookie_add(struct SessionHandle *data, /* rest of params were here */)
{
/* unrelated things were here */
#ifdef CURL_DISABLE_VERBOSE_STRINGS
  (void)data;
#endif
/* rest of function goes here */
}

As you can see, void casted pointer, isn't even assigned to a variable. I was wondering what is the purpose of this.

yasar
  • 13,158
  • 28
  • 95
  • 160

2 Answers2

6

This cast suppresses a compiler warning that would arise if data is not used.

GCC produces this warning if the -Wunused-parameter flag (implied by -Wextra) is enabled.

Joey Adams
  • 41,996
  • 18
  • 86
  • 115
  • What compiler/flags would be needed for that warning? With GCC, I have no issues w/ that. – Richard J. Ross III May 26 '12 at 16:29
  • @RichardJ.RossIII: It appears with `-Wall -W`, but I'm still looking for the exact flag. – Joey Adams May 26 '12 at 16:31
  • This answer doesn't answer the question *Why* - it gives a method of hiding the problem? I suggest that suppressing warnings is not an ideal solution. – Andrew Aug 16 '15 at 08:36
  • @Andrew: This is selective suppression of a warning, so the coder can indicate that a particular function purposely does not use a particular parameter. When you don't use a variable, it's often a mistake or unfinished code, except in some cases (e.g. a callback function where parameters are provided in case you need them). So we want unused warnings except in those special cases. – Joey Adams Aug 17 '15 at 16:54
0

Fair point Joey - but suppressing that warning also suppresses it for all the cases where the programmer has accidentally not used a parameter...

Andrew
  • 2,046
  • 1
  • 24
  • 37