![]() |
statement with no effect?
I would like to correct the following so that gcc no more complains with "warning: statement with no effect" ... if possible:
:
static char *spend;Thanks |
p1 is a pointer to some value. You're trying to decrement the address, instead of the value, when you go: "p1--".
:
(*p1)--;I'd also like to note that... - Your for loop arguments aren't in the correct order. It goes (initiation ; loop condition ; iteration). You have your iteration where the loop condition should be, and vice-versa. - You might need to bracket *p1++, to make (*p1)++. I believe the order of operations needs to derefence first, before incrementing. Try either way, see which one works. |
Sane was right in his notes - the reason you are getting the error is because the for loop needs to be reversed.
However, I dont think you want to be decrementing the value, rather than the pointer as it looks like this function should be using pointer arithmetic. This depends on what your function is supposed to be doing. Something like: :
static char *spend; |
Oh, so it could be pointer arithmetic? All right. I guess "*p1++" kind of threw me off. In that case, you want "p1 < spend" to be "*p1 < spend", unless spend happens to be a pointer as well, in which case that would be some pretty weird programming. o_O
Then you'll also need to change "*p1++" to "p1++". |
spend is a pointer - it is right there in the code.
|
Oh fuck! Hahaha. I'm awesome like that. Sorry. :p
Heh. Thanks for pointing that out. In an attempt to redeem myself, I will post what I believe to be the "correct" code... haha. :
static char *spend; |
Ok thanks ...
For now I will just remove "p1 < spend", and see how it goes... Thanks bye |
Maybe you didn't understand the implications of your responses, what with quite a bit of chaff being bandied about. The for loop works like this:
for (initial value, if any ; condition for repeating the loop ; action at end of loop) because you had it out of order, thusly: for (; *p1++; p1 < spend) There was no initialization (intentional, I'm sure, and allowed) The continuation condition was *p1++, which might or might not evaluate to true. The action was no action at all, rather just an expression (p1 < spend). Evaluation of this expression had no effect on action of the code. Thus the message. Warnings are issued when the compiler detects something that is strange enough to suggest that there is actually a mistake on the programmer's part, but there is no real violation of the rules. They are actually "thinking errors", most of the time. Such appears to be the case here. Write your for correctly, the warning will go away. |
I'll go further than Dawei. The construct "for (; *p1++; p1 < spend)" is technically valid, but is a rather atypical usage of a for loop (it is normally expected that the action at the end of the loop will have a detectable effect; your compiler is moaning because "p1 < spend" does not). If that code actually does what you intend (I sort of doubt that, but it might), you might want to restructure the code in another form (eg a do/while loop).
Just arbitrarily removing code that makes a compiler complain is not good practice. It is better to understand why the compiler is complaining -- in particular, by considering if the complete body of code is actually doing what you intend. |
Code is not mine, it's from minised project...
Here is the whole function: :
/* write a hex dump expansion of *p1... to fpThis will give you a better idea of what's going on... Bye |
| All times are GMT -5. The time now is 12:55 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC