Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C (http://www.programmingforums.org/forum60.html)
-   -   statement with no effect? (http://www.programmingforums.org/showthread.php?t=11667)

iradic Oct 22nd, 2006 8:10 PM

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;

static void listto(char *p1, FILE *fp)
{
        p1--;
        for (; *p1++; p1 < spend)
        ....


Thanks

Sane Oct 22nd, 2006 8:14 PM

p1 is a pointer to some value. You're trying to decrement the address, instead of the value, when you go: "p1--".

:

(*p1)--;
Try that. It derefences the pointer first before decrementing the value.

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.

The Dark Oct 22nd, 2006 10:41 PM

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++ )
          ....
}


Sane Oct 22nd, 2006 11:20 PM

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++".

The Dark Oct 22nd, 2006 11:24 PM

spend is a pointer - it is right there in the code.

Sane Oct 22nd, 2006 11:25 PM

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++)
        ....


iradic Oct 23rd, 2006 10:27 AM

Ok thanks ...

For now I will just remove "p1 < spend", and see how it goes...

Thanks bye

DaWei Oct 23rd, 2006 10:48 AM

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.

grumpy Oct 23rd, 2006 11:43 AM

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.

iradic Oct 23rd, 2006 2:13 PM

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


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