3

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 ? 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),

1
  • This code won't compile. And you haven't initialised cnt to point at any memory. Commented Jan 31, 2012 at 0:49

2 Answers 2

6
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):

  1. ++i: i + 1 is passed and is also the value i takes afterwards.
  2. i++: i is passed and i = i + 1 after the call.
  3. i+1: i + 1 is passed but i remains as just i afterwards.

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I didn't realise the recursive functions needed work too. Oh yeah they just stack right up in infinite recursion.
You should be getting a compilation warning - type mismatch on argument 3 of call to f() in main(). You are passing an int ** where the function expects an int *.
2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.