简体   繁体   English

试图了解一个递归函数来反转一个字符串

[英]Trying to understand a recursive function to reverse a string

I can't understand why this code works. 我不明白为什么这段代码有效。 Do characters in the string get removed because they are being passed by reference? 字符串中的字符是否由于引用传递而被删除? I see no decrement or such, so I find that really confusing. 我没有看到减量之类的东西,所以我感到非常困惑。 Also, is the string getting reversed because of the lifo nature of the function call stack? 另外,由于函数调用栈的lifo性质,字符串是否会反转?

 void reverse(const char * const sPtr){
   if('\0' == sPtr[0])
     return;
     else{
       reverse(&sPtr[1]);
       putchar(sPtr[0]);
    }
 }

Nothing is being removed . 什么也没有被删除 But the pointer being passed into the function is changed, in this expression: 但是传递给函数的指针在以下表达式中发生了变化:

&sPtr[1]

… which is equivalent to …相当于

sPtr + 1

So each recursive call increments the pointer by one, thus causing the recursive calls to traverse your char array. 因此,每个递归调用都将指针加1,从而导致递归调用遍历char数组。

As for why this causes the reversal of the string, the LIFO nature of the stack is indeed the reason. 至于为什么这会导致字符串反转,堆栈的LIFO性质确实是原因。 Your function first calls itself recursively and then outputs the current character using putchar . 您的函数首先递归调用自身, 然后使用putchar输出当前字符。 This has thus the effect of outputting the characters in reverse order. 因此,这具有以相反顺序输出字符的效果。

If you input "abc", the call stack will look something like this. 如果输入“ abc”,则调用堆栈将如下所示。

reverse("abc")
    reverse("bc")
        reverse("c")
        print("c")
    print("b")
print("a")

So each reverse call calls itself with the same string, but first character excluded, and THEN prints the first character in the string. 因此,每个反向调用都使用相同的字符串调用自身,但排除了第一个字符,然后THEN打印该字符串中的第一个字符。

Do characters in the string get removed because they are being passed by reference? 字符串中的字符是否由于引用传递而被删除?

There is no call by reference in C. Pointers kind of emulate it, but everything in C is passed by value. C中没有按引用进行调用。指针有点模仿它,但是C中的所有内容都是按值传递的。 Besides, the type of the pointer const char * const says two things. 此外,指针的类型const char * const表示两件事。 First, the pointer will not get reassigned to point at anything else, but more importantly, it also says that the string will not be changed. 首先,不会重新分配指针指向其他任何地方,但是更重要的是,它还表示不会更改字符串。 Try adding the line sPtr[0] = 'a' somewhere, and you'll get a compiler error. 尝试在sPtr[0] = 'a'添加sPtr[0] = 'a' ,您将得到编译器错误。

No, it doesn't get deleted. 不,它不会被删除。

In every recursive call, you want to progress to the next character, which is done here: 在每个递归调用中,您都希望前进到下一个字符,这是在这里完成的:

reverse(&sPtr[1]); // you pass a parameter the 1st character of where you point to now

After the last reverse(&sPtr[1]) there will be stacked 3 calls of putchar. 在最后一个reverse(&sPtr[1]) ,将堆叠3个putchar调用。

Each call of reverse grows the pointer with 1, so the stacked calls will be --supposing the input string is made of 3 characters-- 每次reverse调用都会使指针增加1,因此堆叠的调用将-假设输入字符串由3个字符组成-

CURRENT FRAME -- *(ptr+3) is NULL
putchar(*(ptr+2))
putchar(*(ptr+1))
putchar(*(ptr+0))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM