-1

This is a sample snippet from a book. I am not understanding how it is printing the string in reverse. The first time reverse is called, the element at 0 is not null assuming of course a valid string was entered. In the else statement, we are calling the function again passing in the address of element at index 1.

So let's say I input "hello". Then I am passing the address of e. The second time reverse is called, however, how is the string being traverse? The index being checked is still 1. Also, it seems to me we are not replacing the values of the array. When we start printing the values with putchar I get so confused that the value at index 0 starts with the last element of the array. I understand that when we use recursion and we hit the base case, and since the items are put on the stack, we start reading them from the top of the stack pointer until the end which is why is in reverse. But here I don't see that happening.

The hard-coded indexes at

reverse( &ptr[1]);
putchar(ptr[0]);

do not make sense to me. I guess I am having difficulty seeing how the string is being traverse when it is the same address being passed each time in the recursion. Please help me understand what is happening.

I have modified the code a bit for simplicity but it does the same thing the one in the book does.

#include <stdio.h>

void reverse(char * ptr);

int main(){
    char sentence[10];
    printf("enter text \n");
    fgets(sentence, 10, stdin);
    printf("The line is: \n");
    reverse(sentence);
    return 0;
}

void reverse( char * ptr){
    if(ptr[0] == '\0'){
        return;
    } else{
        reverse( &ptr[1]);
        putchar(ptr[0]);
        printf("\n");
    }
}
Jongware
  • 22,200
  • 8
  • 54
  • 100
Kinozato
  • 13
  • 1
  • 6
  • You pass `&ptr[1]` to the recursion, so each time the string passed begins with the next character. – Weather Vane Dec 31 '18 at 16:56
  • 1
    I suggest you take some time to learn how to use a debugger. With one you can step through code line by line, and step into function calls, all while monitoring variables and their values. If you do that with the recursive calls, keeping an eye on `ptr[0]` and `ptr[1]`, it should hopefully become clear what's happening. – Some programmer dude Dec 31 '18 at 16:56
  • @Someprogrammerdude Oh shoot! Of course. I know how to use the debugger I just didn't think about GDB. I'll do that right now! – Kinozato Dec 31 '18 at 17:00
  • @WeatherVane Thanks. I will use a debugger to look into this. – Kinozato Dec 31 '18 at 17:01
  • Ahhh this is so cool!! I have never done recursion this way in C as I started out with Java. I see now what is happening. Is not that we are traversing the string in the traditional way but by sending in the starting address each time index at 0 and 1 change because we are "eating up" the array as we go. Thanks for the responses :) – Kinozato Dec 31 '18 at 17:34
  • Go ahead and self-answer this question, and accept the solution. – jxh Dec 31 '18 at 17:45
  • And don't add "(Solved)" or "(Answered)" or something like that to your title. – Jongware Dec 31 '18 at 18:37

1 Answers1

0

The &ptr[1] is equivalent to ptr + 1. Now, if you think of ptr as a number (which it is, really) it should be obvious that it's not the same number being passed every time, but rather one greater for each level of recursion. It's basically passing a substring starting from the second character, only in C the substring is not a separate object but rather a pointer to a different position in the same string.

Arkku
  • 41,011
  • 10
  • 62
  • 84