简体   繁体   中英

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. 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. 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 . (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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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