Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 22nd, 2006, 7:10 PM   #1
iradic
Programmer
 
Join Date: Feb 2006
Posts: 36
Rep Power: 0 iradic is on a distinguished road
Thumbs up 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
iradic is offline   Reply With Quote
Old Oct 22nd, 2006, 7:14 PM   #2
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,827
Rep Power: 5 Sane will become famous soon enough
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.
Sane is offline   Reply With Quote
Old Oct 22nd, 2006, 9:41 PM   #3
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 824
Rep Power: 4 The Dark is on a distinguished road
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++ )
           ....
}
The Dark is offline   Reply With Quote
Old Oct 22nd, 2006, 10:20 PM   #4
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,827
Rep Power: 5 Sane will become famous soon enough
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++".
Sane is offline   Reply With Quote
Old Oct 22nd, 2006, 10:24 PM   #5
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 824
Rep Power: 4 The Dark is on a distinguished road
spend is a pointer - it is right there in the code.
The Dark is offline   Reply With Quote
Old Oct 22nd, 2006, 10:25 PM   #6
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,827
Rep Power: 5 Sane will become famous soon enough
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.
Sane is offline   Reply With Quote
Old Oct 23rd, 2006, 9:27 AM   #7
iradic
Programmer
 
Join Date: Feb 2006
Posts: 36
Rep Power: 0 iradic is on a distinguished road
Ok thanks ...

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

Thanks bye
iradic is offline   Reply With Quote
Old Oct 23rd, 2006, 9:48 AM   #8
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Oct 23rd, 2006, 10:43 AM   #9
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,206
Rep Power: 5 grumpy is on a distinguished road
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.
grumpy is offline   Reply With Quote
Old Oct 23rd, 2006, 1:13 PM   #10
iradic
Programmer
 
Join Date: Feb 2006
Posts: 36
Rep Power: 0 iradic is on a distinguished road
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
iradic is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:21 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC