![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Feb 2006
Posts: 36
Rep Power: 0
![]() |
I would like to correct the following so that gcc no more complains with "warning: statement with no effect" ... if possible:
static char *spend;
static void listto(char *p1, FILE *fp)
{
p1--;
for (; *p1++; p1 < spend)
....Thanks |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,827
Rep Power: 5
![]() |
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. |
|
|
|
|
|
#3 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 824
Rep Power: 4
![]() |
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;
static void listto(char *p1, FILE *fp)
{
p1--;
for (; p1 < spend; p1++ )
....
} |
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,827
Rep Power: 5
![]() |
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++". |
|
|
|
|
|
#5 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 824
Rep Power: 4
![]() |
spend is a pointer - it is right there in the code.
|
|
|
|
|
|
#6 |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,827
Rep Power: 5
![]() |
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;
static void listto(char *p1, FILE *fp)
{
p1--;
for (; p1 < spend; p1++)
....Last edited by Sane; Oct 22nd, 2006 at 10:38 PM. |
|
|
|
|
|
#7 |
|
Programmer
Join Date: Feb 2006
Posts: 36
Rep Power: 0
![]() |
Ok thanks ...
For now I will just remove "p1 < spend", and see how it goes... Thanks bye |
|
|
|
|
|
#8 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#9 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,206
Rep Power: 5
![]() |
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. |
|
|
|
|
|
#10 |
|
Programmer
Join Date: Feb 2006
Posts: 36
Rep Power: 0
![]() |
Code is not mine, it's from minised project...
Here is the whole function: /* write a hex dump expansion of *p1... to fp
p1: the source
fp: output stream to write to */
static void listto(char *p1, FILE *fp)
{
p1--;
for (; *p1++; /* p1<spend */ ) /* no effect ? */
if (isprint(*p1))
putc(*p1, fp); /* pass it through */
else
{
putc('\\', fp); /* emit a backslash */
switch(*p1)
{
case '\b': putc('b', fp); break; /* BS */
case '\t': putc('t', fp); break; /* TAB */
case '\n': putc('n', fp); break; /* NL */
case '\r': putc('r', fp); break; /* CR */
case '\033': putc('e', fp); break; /* ESC */
default: fprintf(fp, "%02x", *p1 & 0xFF);
}
}
putc('\n', fp);
}This will give you a better idea of what's going on... Bye |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| SQL Select Statement Performance | King | Other Scripting Languages | 6 | Dec 1st, 2006 5:12 PM |
| Why did %S break my if statement? | somehollis | Python | 3 | Jun 22nd, 2006 8:49 PM |
| The Genie Effect - "Windows" | Kilo | Coder's Corner Lounge | 3 | Apr 4th, 2006 9:29 AM |
| Trouble with a swith statement | cjaime | C | 10 | Nov 8th, 2005 11:09 AM |
| Image distortion effect | djm_2k1 | Java | 0 | Oct 11th, 2005 1:21 PM |