SunQuest
           C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old February 17th, 2003, 07:22 AM
balance balance is offline
.
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Posts: 296 balance User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
is this extra pointer variable necessary?

just learning pointers. is this the way to do this?:
Code:
/* reverse: reverse s string in place (pointer version) */
void reverse(char *s)
{
	char *t=s;
	char *u=s;		/* this *u pointer */
	char c;

	while(*++t)
		;
	while(t>=u) {		/* for use here */
		c=*s;
		*s++=*t;
		*t--=c;
	}
}


the part i'm wondering about in particular is the need for *u which is for use as a static marker (static in the english way, not c) to remember where the start is.

i'm not looking for a long-winded way to avoid using this *u pointer. i've come accross this situation a few times, so i just want to confirm this is a reasonable/good way to do it, or if i've missed something obvious? i think it's probably fine and *u is essential in this situation, but i just wanted to check. thanks.

(this site has been doing a lot of this recently: Warning : mysql_pconnect() [ function.mysql-pconnect ]: Can't connect to local MySQL ser... :/ )

Reply With Quote
  #2  
Old February 17th, 2003, 01:20 PM
M.Hirsch M.Hirsch is offline
Contributing User
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Oct 2000
Location: Back in the real world.
Posts: 5,969 M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 22 h 39 m 55 sec
Reputation Power: 184
from what i see, you are using *u to make sure you donīt read beyond the first character of s because you are modifying *s. (beyond backwards => you wonīt read the -1th character).
__________________
--
Manuel Hirsch - Linux, FreeBSD, programming, administration articles, tutorials and more.

Reply With Quote
  #3  
Old February 17th, 2003, 04:20 PM
3dfxMM 3dfxMM is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 266 3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 3 Days 18 h 56 m 13 sec
Reputation Power: 12
The way you are using u will cause this code to reverse the string and then put it back the way it was. You have t and s working in from each end which is great, but then you keep going all the way to the beginning of the string instead of stopping when s==t. Also, this code will start stomping around in memory if you pass in "". The bottom line is you don't need u.

Reply With Quote
  #4  
Old February 17th, 2003, 07:05 PM
balance balance is offline
.
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Posts: 296 balance User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
wow! :/ - i hadn't realised it didn't even work. i posted this thinking i'd got it working and was just asking a polishing off question, but the original thing didn't even work in the first place. unbelieveable! don't know how that happend. very annoying (and embarrasing). i've fixed it now but..


*another* thing that was wrong, this part:
Code:
while(*++t)
	;

needed to be like this:
Code:
while(*t)
	t++;
t--;

in order to point to the char before the \0, to leave it in the same place, untouched.

if the s[] char array looks like this:

s[0] = 'a'
s[1] = 'b'
s[2] = 'c'
s[3] = '\0'

i need the *t pointer to end up pointing to s[2], not s[3].

so why doesn't...:
Code:
while(*++t)
	;

...result with *t pointing to s[2] and not s[3] as it does? i would have thought it would have as the ++ is prefix in relation to t rather than post. also this doesn't work either:
Code:
while(*(++t))
	;

is
Code:
while(*t)
	t++;
t--;

the only way to say that?

thanks

Reply With Quote
  #5  
Old February 17th, 2003, 11:28 PM
3dfxMM 3dfxMM is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 266 3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 3 Days 18 h 56 m 13 sec
Reputation Power: 12
Quote:
so why doesn't...:
Code:
while(*++t)
	;
...result with *t pointing to s[2] and not s[3] as it does? i would have thought it would have as the ++ is prefix in relation to t rather than post.
You actually wrote the answer right there. The ++ is prefix which means that t is incremented before it is dereferenced.

Quote:
also this doesn't work either:
Code:
while(*(++t))
	;
This is exactly the same as while(*++t);

Quote:
is
Code:
while(*t)
	t++;
t--;
Yes, but it still won't work properly if "" is passed to it.

Reply With Quote
  #6  
Old February 18th, 2003, 11:41 AM
balance balance is offline
.
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Posts: 296 balance User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Quote:
You actually wrote the answer right there. The ++ is prefix which means that t is incremented before it is dereferenced.


but what i was hoping for, which i now realise was wishfull thinking, was for after that while loop the t pointer would point to the char variable one before the '\0' end of string marker. but of course if ++ is postfix or prefix it still actually increments t regardless, so after that loop, t points to the same thing. i was hoping for it to kind of look forward, as it were, and stop one before but i wasn't thinking clearly.

it just means that the t pointer just needs to be knocked back one with t--; after that loop, having got the pointer pointing to the final '\0' char, which is no big deal.

(i don't want to move the '\0' end marker because this function reverses the characters and will result in '\0' in the same place so best just to leave it there)

when you say "" that's the same as '\0' right?

thanks





Code:
/* reverse: reverse s string in place (pointer version) */
void reverse(char *s)
{
	char *t=s;
	char c;
	while(*t)			/* could be while(*++t) ; */
		t++;			/* no difference. */
	t--;				/* still need this line */
	while(t>=s) {
		c=*s;
		*s++=*t;
		*t--=c;
	}
}

Reply With Quote
  #7  
Old February 19th, 2003, 12:29 PM
3dfxMM 3dfxMM is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 266 3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 3 Days 18 h 56 m 13 sec
Reputation Power: 12
Quote:
when you say "" that's the same as '\0' right?

Practically speaking, yes, but actually '\0' is a character whose value is 0. "" is an empty string. Since it is empty, its first character is '\0'.
Code:
while(*t) t++;
is not the same as
Code:
while(*++t);
The first one will correctly fall out of the loop with t pointing to the '\0' character. The second one will go past the '\0' character if it is the first character in the string. In the first case you will need to check to make sure that it is safe to do the t--. The second one will just flat not work correctly if "" is passed in.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > is this extra pointer variable necessary?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway