1

I have a function that takes a string,and reverses it, using front and rear pointers. Now this is the program written below. what I do not understand is the while loop. When it assigns *front = *rear , and *rear = temp //which is *front. how do we still increment and decrement front and rear respectively. But we switched them didn't we ? isn't front in the rear and rear in the front now ? or is it because it's pointers ? can someone explain this please to me ?

Program:

#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;

void reverse(char* s);
int main()
{
    char aString[] = "babylonia";

    cout << " please enter the string " << endl;

    reverse(aString);

    cout << aString << endl;


}

void reverse(char* s)
{
    char *front, *rear, temp;
    front = s;
    rear = s+strlen(s) - 1;
    while( front < rear)
    {
        temp = *front;
        *front = *rear;
        *rear = temp;
        front++;
        rear--;
    }

}
matrixCode
  • 111
  • 1
  • 2
  • 7
  • 5
    What are swapped are the values, not the pointers. What are in-/de-cremented are the pointers, unrelated to the values. –  Mar 02 '15 at 03:48
  • 1
    But its swapping *front and *rear, not front and rear, doesn't that mean it's swapping the pointers ? or when assigning a pointer you are assigning the value of it ? – matrixCode Mar 02 '15 at 03:50
  • 5
    `front` and `rear` are pointers. `*front` and `*rear` are the values pointed to by the pointers. – eigenchris Mar 02 '15 at 03:51
  • 5
    well a `*` before a pointer dereferences it. Which means the value pointed to by the pointer is being accessed. – shiraz Mar 02 '15 at 03:51
  • Little bit confusing. But I think I understand. – matrixCode Mar 02 '15 at 04:07
  • 1
    Imagine you are doing this in real life with some Scrabble tiles on a scrabble board. The two pointers are your left and right hand (when you are preparing for a swap -- not during a swap). You grab the leftmost tile with your left hand and the rightmost tile with your right hand, and swap the tiles over then return your hands to where they were. Then you move your hands along by one and repeat. – M.M Mar 02 '15 at 05:27
  • Wow, the answer given in comments several times. Good job guys – Lightness Races in Orbit Mar 02 '15 at 11:11

1 Answers1

1

front = s // which is the same as front = &s[0], points to the address of the first character in the array.
And rear, which points to the address of the last character in the array.

(These are just example addresses)
|0x23fde8 | 0x23fdf0| 0x23fdf8| 0x23fe00| 0x23fe08| 0x23fe10| 0x23fe18| 0x23fe20| 0x23fe28|
.---------.---------.---------.---------.---------.---------.---------.---------.---------.
|    B    |    A    |    B    |    Y    |    L    |    O    |    N    |    I    |    A    |
'---------'---------'---------'---------'---------'---------'---------'---------'---------'
     ^                                                                               ^

At first, front points to an address for example 0x23fde8 and rear points to 0x23fe28.
Now, when you dereference it with the asterisk you can access that part of the memory, you can imagine a container as illustrated above or you can imagine that you open the container with the asterisk *

When you increment front by doing front++ or ++front, it increments to the next memory address location. At the same time asrear-- or --rear decrements to next address location. It does not change the address, but when you dereference it you can access the content.

Below you see that the first and the last have been swapped, but the addresses remain the same.

|0x23fde8 | 0x23fdf0| 0x23fdf8| 0x23fe00| 0x23fe08| 0x23fe10| 0x23fe18| 0x23fe20| 0x23fe28|
.---------.---------.---------.---------.---------.---------.---------.---------.---------.
|    A    |    A    |    B    |    Y    |    L    |    O    |    N    |    I    |    B    |
'---------'---------'---------'---------'---------'---------'---------'---------'---------'
               ^                                                           ^
             front                                                        rear

I hope that this helps you to understand the difference between *front and front

Andreas DM
  • 10,685
  • 6
  • 35
  • 62