|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
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
|
|||
|
|||
|
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... :/ ) |
|
#2
|
|||
|
|||
|
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. |
|
#3
|
|||
|
|||
|
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.
|
|
#4
|
|||
|
|||
|
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 ![]() |
|
#5
|
|||||
|
|||||
|
Quote:
Quote:
Quote:
|
|
#6
|
|||
|
|||
|
Quote:
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;
}
}
|
|
#7
|
|||
|
|||
|
Quote:
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++; Code:
while(*++t); |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > is this extra pointer variable necessary? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|