简体   繁体   English

c将指针传递给递归函数

[英]c passing pointer to recursive function

So here i am struggling with this program, i was trying to find out how can I use a array of pointers declared into main, in a recursive function to memorize data, the question that arises here is if it's the same approach as for a single pointer, what about for a struct type ? 所以在这里我正在努力学习这个程序,我试图找出如何使用一个声明为main的指针数组,在一个递归函数中记忆数据,这里出现的问题是,如果它与单个方法相同指针,结构类型怎么样? what is the best way to pass by reference a variable/array to a recursive function ? 通过引用变量/数组传递给递归函数的最佳方法是什么?

#include <stdio.h>
#include <stdlib.h>
#define N 1

void f(int i,int j,int *cnt);

int j=0;

int main(int argc, char *argv[])
{
  int *cnt=0;
  f(0,++j,&cnt);
  printf("------ %d ---- \n",cnt);
  system("PAUSE");  
  return 0;
}

void f(int i,int j,int *cnt){

   if(i>N){
          printf("---if --- %d ---- %d \n",i,j);
          (*cnt)++;
          return;
          }

   (*cnt)++;
   printf("---bg --- %d ---- %d \n",i,j);
   f(i+1,++j,cnt);
   f(i+1,++j,cnt);        
}

Another thing i'd like to know is how does the recursive functions handle the ++i and i++ and i+1 increments (when passed as parameters), 另一件我想知道的是递归函数如何处理++ i和i ++以及i + 1增量(当作为参数传递时),

int main(int argc, char *argv[])
{
    int *cnt=0;
    f(0,++j,&cnt);
    printf("------ %d ---- \n",(*cnt));
    system("PAUSE");  
    return 0;
}

needs to be 需要是

int main(int argc, char *argv[])
{
    int intStorage = 0;//<---- As Oli said.
    int *cnt= &intStorage;
    f(0,++j,cnt);//<-------AMPERSAND removed, overly dereferenced.
    printf("------ %d ---- \n",(*cnt));
    system("PAUSE");  
    return 0;
}

++i and i++ and i+1 (when passed as parameters): ++ i和i ++和i + 1(当作为参数传递时):

  1. ++i: i + 1 is passed and is also the value i takes afterwards. ++ i:i + 1被传递,也是我之后获得的值。
  2. i++: i is passed and i = i + 1 after the call. i ++:在通话结束后我通过,i = i + 1。
  3. i+1: i + 1 is passed but i remains as just i afterwards. i + 1:i + 1通过,但我之后仍然如此。

I'll try and fix your function a little too: 我会尝试修复你的功能:

void f(int i,int j,int *cnt){

    if(i>N){
        printf("---if --- %d ---- %d \n",i,j);
        return;
    }

    (*cnt)++;
    printf("---bg --- %d ---- %d \n",i,j);
    if ( i < 50 && j < 50 ) {
        f(i+1,++j,cnt);
        f(i+1,++j,cnt);
    }
}

Still a lot of recursion but without the danger of not stopping. 仍然有很多递归,但没有不停止的危险。

A simple way of handling the pointer into your function is: 处理指向函数的指针的简单方法是:

#include <stdio.h>
#include <stdlib.h>
#define N 1

void f(int i, int j, int *cnt);

int j = 0;

int main(void)
{
    int cnt = 0;
    f(0, ++j, &cnt);
    printf("------ %d ----\n", cnt);
    return 0;
}

void f(int i, int j, int *cnt)
{
   // Having a local variable j and a global j is likely to confuse someone!
   if (i > N)
   {
      printf("---if --- %d ---- %d\n", i, j);
      return;
   }

   (*cnt)++;
   printf("---bg --- %d ---- %d\n", i, j);
   f(i+1, ++j, cnt);
   f(i+1, ++j, cnt);
}

This code produces the following output with no crash: 此代码生成以下输出而不会崩溃:

---bg --- 0 ---- 1
---bg --- 1 ---- 2
---if --- 2 ---- 3
---if --- 2 ---- 4
---bg --- 1 ---- 3
---if --- 2 ---- 4
---if --- 2 ---- 5
------ 3 ----

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

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