++char

I'm trying to understand how this piece of code works.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void reverse(char *str){
     char * end = str; 
     char tmp; 
     if(str) {
          while(*end){
               ++end; 
          }
          --end;
          while(str < end){
               tmp = *str;
               *str++ = *end; 
               *end-- = tmp; 
          }
      }
}


Its supposed to reverse a C-Style string. I don't understad what ++end and --end acomplishes, how does that help reverse the string?
Last edited on
The end pointer, points to the first element of the array (i.e: str). The increment operator (i.e: ++end), will make end to point to the next element of the string. As you can guess, --end goes in the opposite direction. To be more blunt, those commands are used to traverse the string.
Last edited on
so the parameter (str < end) means that the while loop runs while pointer to str is smaller than the end pointer?
Topic archived. No new replies allowed.