简体   繁体   English

如何在递归函数中将指针变量从被调用函数传递给调用函数?

[英]how to pass a pointer variable from the called function to the calling function in a recursion function?

in C the called function cannot directly alter a variable in the calling function, it can only alter its private, temporary copy.在 C 中,被调用函数不能直接更改调用函数中的变量,它只能更改其私有的临时副本。 so I use the pointer variable to alter and to pass the variable in the called function to the calling function.所以我使用指针变量来改变并将被调用函数中的变量传递给调用函数。 But in a recursion function, the function calls itself.但在递归函数中,函数调用自身。 In the first recursion, I use a pointer, and in the second recursion, the function asks for a pointer that points to the prior pointer.在第一次递归中,我使用了一个指针,在第二次递归中,该函数要求一个指向前一个指针的指针。 And in the next recursion, a pointer that points to the pointer in the second recursion is asked.并且在下一次递归中,会询问指向第二次递归中的指针的指针。 How to avoid this condition since my aim is to pass the variable that is created in the called recursion function?如何避免这种情况,因为我的目标是传递在被调用的递归函数中创建的变量?

given the data of a node, I want to search and alter the node in a binary search tree.给定节点的数据,我想在二叉搜索树中搜索和更改节点。 I use the pointer variable aPointerToNode to locate the node, but when I use the recursive function SearchBST , I pass a pointer that points to aPointerToNode so that I can alter it in the called function.我使用指针变量aPointerToNode来定位节点,但是当我使用递归函数SearchBST ,我传递了一个指向aPointerToNode的指针,以便我可以在被调用的函数中更改它。 But when the recursive function calls itself, the function asks for another pointer that points to the prior pointer.但是当递归函数调用自身时,该函数会要求另一个指向前一个指针的指针。 If I give the function the prior pointer but not another pointer that points to the prior pointer, the function will not return the node that I search for, that is, it just creates a temporary copy and returns nothing(I want to use the arguments but not the return value of the function to pass the variable).如果我给函数一个先验指针而不是另一个指向先验指针的指针,该函数将不会返回我搜索的节点,也就是说,它只是创建一个临时副本并且不返回任何内容(我想使用参数但不是传递变量的函数的返回值)。

#include<stdio.h>

struct t_Data
{
  int m_Info;
};

struct t_Node
{
  struct t_Data m_Data;
  struct t_Node* m_LeftChild;
  struct t_Node* m_RigthChild;
};

typedef struct t_Node* t_BinarySortTree;

void SearchBST(t_BinarySortTree T,int aGivenInfo, struct t_Node* *result)
{
  if(aGivenInfo == (*T).m_Data.m_Info)
  {
    (*result) = T;
  }
  else if (aGivenInfo < (*T).m_Data.m_Info)
  {
    SearchBST((*T).m_LeftChild,aGivenInfo,result);
  }

  /* condition: aGivenInfo > (*T).m_Data.m_Info */
  else
  {
    SearchBST((*T).m_RightChild,aGivenInfo,result);
  }
}

void main(void)
{
  t_BinarySortTree aBST;
  aBST = NULL;

  int targetInfo;
  targetInfo = 58;

  struct t_Node* aPointerToTargetNode;
  aPointerToTargetNode = NULL;


  SearchBST(aBST,targetInfo,&aPointerToTargetNode); 
}

finally, in the function main() , the variable aPointerToNode points to the node that has the targetInfo .最后,在函数main() ,变量aPointerToNode指向具有targetInfo的节点。 (I omit the creation of the binary search tree for the clearness of the question) (为了问题的清晰,我省略了二叉搜索树的创建)

You don't need pointer to pointer to pointer ... to pointer.您不需要指向指针的指针...指向指针。 The base pointer doesn't change基指针不变

#include <stdio.h>
void rec(int *p, int n) {
    if (n == 0) return;
    *p += n;
    rec(p, n - 1);
}
int main(void) {
    int sum = 0;
    rec(&sum, 100);
    printf("sum is %d\n", sum);
}

See code running on ideone查看ideone 上运行的代码

与其在递归函数中传递变量,不如让它成为全局变量。

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

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