0

I saw the piece of the code. But do not know what the purpose is.

void a()
{
  static const char *string = "STRING";
  ...
  (void)string;  // <---- What the purpose of the line is?
  ...
}

1 Answers1

0

(void) before a variable like that creates an empty expression and is used to silence warnings about a variable not being used by the program.

In this specific case it would be better to simply comment out the variable declaration.

Lundin
  • 155,020
  • 33
  • 213
  • 341
  • It's possible that the string has been defined like this for debugging or other purposes (e.g. embedded version or copyright string), i.e. so that it shows up in the executable. – Paul R Apr 24 '20 at 08:07
  • @PaulR Not really. If you want a dead string that just sits in the executable, it should have been declared at file scope and as `volatile`. Because local `const` variables tend to end up on the stack and as such are more prone to getting optimized out. – Lundin Apr 24 '20 at 08:13
  • You're right for pretty much any decent modern compiler, but it could be ancient code, or it could be that the author's intent did not match up with reality. I was just hazarding a guess as to what the original intent might have been. – Paul R Apr 24 '20 at 08:57