Thread: question on recursive function

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    35

    question on recursive function

    Hi guys

    Code:
    #include<stdio.h>
    
    
    void p(int n){
       
       if(n > 0){
         p(n - 2); 
         printf("%d ", n); 
         p(n - 1);
        }
    } 
    int main(void) {
       p(4);
       
    return 0;   
    }
    explain me plz: why does it print :2 1 4 1 3 2 1

    Also can you explain me what is this statement:
    int (*func())[20];


  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Walk through it on paper next time:
    Code:
    p(4)
        4>0 so...
        p(2)
            2>0 so...
            p(0)
                p(0), 0!>0 so it returns
            print 2
            p(1)
                1>0 so...
                p(-1), -1!>0 so it returns
                print 1
                p(0), 0!>0 so it returns
        print 4
        p(3)
        ...you do the rest...
    As for:
    Code:
    int (*func())[20];
    This is trying to be an array of 20 function pointers.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    35
    thanks a lot

    another thing...
    can you implement int (*fuct())[20]
    in an example to understand what is going on(plz)?

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >This is trying to be an array of 20 function pointers.
    No, it's a function returning a pointer to an array of 20 integers. An array of 20 pointers to function returning int would look like this:
    Code:
    int (*func[20])();
    >can you implement int (*fuct())[20]
    >in an example to understand what is going on(plz)?
    Code:
    #include <stdio.h>
    
    int (*func())[20];
    
    int
    main(void)
    {
      int (*array)[20] = func();
      int   i;
    
      for (i = 0; i < 20; i++) {
        printf("%d\n", (*array)[i]);
      }
    
      return 0;
    }
    
    int (*func())[20]
    {
      static int a[20];
      int        i;
    
      for (i = 0; i < 20; i++) {
        a[i] = i;
      }
    
      return &a;
    }
    My best code is written with the delete key.

  5. #5
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    Is there anything wrong in the code to allocate arrays of a particular length below. I have never returned pointer to arrays before so I wanted to be sure.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef int array_20[20];
    
    array_20 *
    func(void)
    {
      array_20 *a;
      int i;
    
      a= malloc(sizeof *a);
    
      for(i=0;i < sizeof *a/sizeof (*a)[0];i++)
        {
            (*a)[i]=i;
        }
    
      return a;
    }
    
    
    int
    main(void)
    {
      array_20 *array;
      int   i;
    
      array=func();
    
      for (i = 0; i < 20; i++)
        {
            printf("%d\n", (*array)[i]);
        }
    
      free(array);
      return 0;
    }
    The one who says it cannot be done should never interrupt the one who is doing it.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Is there anything wrong in the code to allocate arrays of a particular length below.
    No, it's fine. You allocate the memory for an array of size 20 and have a pointer to an array point to that memory. Then later you free it. All is well, but it's easy to see why pointers to arrays aren't used much, look at that syntax.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function name basic question help
    By kenryuakuma in forum C++ Programming
    Replies: 7
    Last Post: 09-24-2008, 07:48 AM
  2. Need help designing a recursive function.
    By broli86 in forum C Programming
    Replies: 3
    Last Post: 07-24-2008, 12:45 PM
  3. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM