-1

I have studied c++ for about 2 weeks on cave of programming (while working full time :( ) and I just got an understanding for pointers. Point *pThis to &this to get the value out of this address and so on. Anyway, I just got to a video that flipped everything I knew.

I had to copy this to paper first because I am not at home I hope it is correct!

char text[] = "hello";

int nChars = sizeof(text) - 1;

char *pStart = text;
char *pEnd = text + nChars - 1;

while(pStart < pEnd)
{
    char save = *pStart;
    *pStart = *pEnd;
    *pEnd = save;

    pStart++;
    pEnd--;
}

cout >> text >> endl;

I hope I wrote it correct from this video with my phone

https://youtu.be/g9oMn4mEx14

I do understand that we are creating two pointers that points to the start and end of text and then switching them and so on. What I don't understand is how can it change text without us even assigning anything to that char array anywhere? This boggles my mind.

I hope someone can help me get this :)

cigien
  • 57,834
  • 11
  • 73
  • 112
V mike
  • 11
  • 1
  • `int i = 5; int *pi = &i; *pi = 6; printf("%d\n", i);` <- prints 6. what????? how can `i` be 6 when we didn't even call that int anywhere? – user253751 Nov 25 '20 at 15:49
  • 2
    The code in the loop does a s swap of values and moves the start and end each 1 step closer to each other. – drescherjm Nov 25 '20 at 15:50
  • 2
    Please note that Stack Overflow expects a certain amount of effort on your part. Please take the time to sit down at a computer when possible, and write the code, and run the program. Posting code with pointers that you wrote by hand is not going to be well received. – cigien Nov 25 '20 at 15:51
  • The changing is done here `*pStart = *pEnd;` and here `*pEnd = save;`. This is normal, you have a pointer, you dereference it and assign. That changes whatever the pointer is pointing to, in this case elements of the `text` array. – john Nov 25 '20 at 15:51

3 Answers3

4

Draw it on paper (still the best method to both understand and write code with pointers).
Here is an ASCII version:

The beginning:

+---+---+---+---+---+---+
| h | e | l | l | o | 0 |
+---+---+---+---+---+---+
  ^               ^
  |               |
 pStart         pEnd

The loop:

save = *pStart;

+---+---+---+---+---+---+
| h | e | l | l | o | 0 |       save: h
+---+---+---+---+---+---+
  ^               ^
  |               |
 pStart         pEnd

Then:

*pStart = *pEnd;

+---+---+---+---+---+---+
| o | e | l | l | o | 0 |       save: h
+---+---+---+---+---+---+
  ^               ^
  |               |
 pStart         pEnd

Then:

 *pEnd = save;

+---+---+---+---+---+---+
| o | e | l | l | h | 0 |       save: h
+---+---+---+---+---+---+
  ^               ^
  |               |
 pStart         pEnd

And then:

pStart++;
pEnd--;

+---+---+---+---+---+---+
| o | e | l | l | h | 0 |       save: h
+---+---+---+---+---+---+
      ^       ^
      |       |
    pStart   pEnd

and you can repeat the loop yourself.
(It's much faster and easier on paper than with a computer and a keyboard.)

molbdnilo
  • 64,751
  • 3
  • 43
  • 82
0

how can it change text without us even assigning anything to that char array anywhere? This boggles my mind.

Because the char array acts like a pointer - it's just an address to where the characters are stored. You want to be changing the characters themselves. The location in memory where the array is remains constant here.

cigien
  • 57,834
  • 11
  • 73
  • 112
Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
0

To simplify this to pseudocode, we are essentially swapping the characters at the start and end pointers using a temp variable save.

In the code snippet you just posted, this occurs in 3 steps:

  • store start to save
  • store end to start
  • store save to end
topgunner
  • 52
  • 5