Thread: Help with structures and recursion

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    2

    Help with structures and recursion

    I'm making this ai program, but I'm having some trouble. I've made a smaller program that shows what what's wrong.

    Code:
    #include <stdio.h>
    
    struct test *tree(struct test *p);
    
    struct test {
      int t;
      struct test *test2;
    };
    
    int j=0;
    
    int main() 
    {
      
      struct test *root;
      
      
      tree(root);
      
      printf("Roots t %d \n",root->t);
      
    }
    
    
    struct test *tree(struct test *p) 
    {
      struct test *malloc();
      
      *p = *malloc(sizeof(struct test));
          printf("Hi\n");
      
      p->t=1;
      if(j==0){
        j++;
        tree(p->test2);
          printf("Test2's t %d\n", p->test2->t);
          
        }
      
        return(p);
    }
    So the main function calls tree, sending root as an argument. It is malloced space for the structure, t is set to 0, then it calls itself with a pointer in root to an instance of itself. It's t is assigned a 1, then it is supposed to be returned, print that it is eaual to 1, then return to the main function and say that its equal to 1. However, it gets a segmentation fault the first time it trys to print p->test2->t.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
     struct test *malloc();
    What are you doing? Don't reprototype malloc.

    Code:
    *p = *malloc(sizeof(struct test));
    Wrong two ways.

    1) You should be passing a pointer to a pointer if you plan on actually updating the pointer outside of your test or tree function. (IE: Making it point to something not allocated.)

    2) You shouldn't be dereferencing malloc.

    Code:
    struct foo *myfun( struct **foo )
    {
        *foo = malloc( sizeof (struct foo) );
        ...do stuff...
    }
    
    ...do stuff...
    
    struct foo *thisonehere;
    
    myfun( &thisonehere );
    Next, you never actually test the return value of malloc. That's wrong. Additionally, you're using a global, which there is no need for. But that should get you started.

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

  3. #3
    Registered User
    Join Date
    Jan 2004
    Posts
    2
    I know that I'm not testing malloc, but I didn't include error checking in this program just to make it simpler. I also don't usually use global variables, but in this case, it was easier to have a global variable rather than passing the function two arguments. I haven't tried your suggestions yet but I will do so soon.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursion... why?
    By swgh in forum C++ Programming
    Replies: 4
    Last Post: 06-09-2008, 09:37 AM
  2. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM