Reputation: 169
A friend of mine asked me the following question, but I was not sure how to answer it...
"Consider this statement:
int* p;
... (lets assume p is set to point to something that makes sense)
*p++;
That statement will first increment p by one, and then dereference it. But the question is, when I compile this, I get this warning:
ex-9-frepe.cc:28:9: warning: value computed is not used [-Wunused-value]
*p++;
... which makes sense. I do not use that value.
In the exercise in question, you're supposed to parenthesize the statement fully, so I did:
(*(p++));
And with that, the warning goes away. That's weird, do you understand why? I'm still not using the value.
With this parenthesization:
*(p++);
... the warning remains."
My assumption is that that the compiler is tricked by the fact that the final value is parenthesized "(*(p++))" and considers that this is considered "using". But I was not sure at all so I decided to bring this topic for a debate...
Upvotes: 3
Views: 135
Reputation: 141598
*p++;
That statement will first increment p by one, and then dereference it
Actually it will dereference the original value of p
.
Also, there is no sequencing relation defined between these two steps; i.e. in general the increment may be performed either before or after, so long as the original value is dereferenced. But in this exact case there is no difference in observable behaviour between the two possible sequencings.
Your other versions of code all have the exact same defined behaviour. The warnings are optional (i.e. not mandated by the C standard); the most likely explanation is that your compiler is trying to be helpful by giving the warning for the first code; and it considers that if you throw in redundant parentheses then you are trying to silence the warning. (This is unfortunately a fairly common technique regarding warnings).
In case this was unclear: the warning is due to the result of the dereferencing operation being unused (not anything to do with the increment); the code *p;
ought to give the same warning.
To get a more exact answer you would need to post your compiler's name and version number.
Another common example of compilers treating redundant parentheses as "metadata" regarding warnings is:
if ( a = b ) // warning, did you mean '==' ?
if ( (a = b) ) // no warning - assume he really did mean '='
Upvotes: 1