#1
  1. No Profile Picture
    .
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Posts
    296
    Rep Power
    13

    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. #2
  3. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Location
    Back in the real world.
    Posts
    5,966
    Rep Power
    191
    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).
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Posts
    272
    Rep Power
    19
    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.
  6. #4
  7. No Profile Picture
    .
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Posts
    296
    Rep Power
    13
    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 :)
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Posts
    272
    Rep Power
    19
    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.

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

    is
    Code:
    while(*t)
    	t++;
    t--;
    Yes, but it still won't work properly if "" is passed to it.
  10. #6
  11. No Profile Picture
    .
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Posts
    296
    Rep Power
    13
    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;
    	}
    }
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Posts
    272
    Rep Power
    19
    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.

IMN logo majestic logo threadwatch logo seochat tools logo