Help with pointers to pointers (**)

This is a discussion on Help with pointers to pointers (**) within the C Programming forums, part of the General Programming Boards category; Hey guys, I am currently learning C in one of my programming classes and I am having an issue with ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    1

    Help with pointers to pointers (**)

    Hey guys,
    I am currently learning C in one of my programming classes and I am having an issue with pointers to pointers.

    The goal of my project is to create a tree of bredth b and depth d

    I have a struct that was defined in skeleton code by my instructor (have to use this format):
    Code:
    struct dir {  char *path;           /* path to get to this dir, not including this */
      char *name;           /* current dir name */
      int d;           /* 3 <= depth level < MAXD */
      int b;           /* 3 <= breadth or number of sub dirs < MAXB */
      struct dir *up;       /* points to the parent */
      struct dir **down; /* points to the sub dirs of this dir. Note the TWO *'s */
      struct dir *left;       /* left sibling dir, use circular link */
      struct dir *right;       /* right sibling dir, use circular link */
    };
    I can set up the tree with everything except the down node implemented and it appears to work.

    In my attempt to figure it out how to get the down node to work I keep getting a segmentation fault 11.

    Code:
    /* create directories breadth first */void create_dir_tree(struct dir **hp,char *name,int d, int b) {
          //printf("create_dir_tree init_d=%s depth=%d breadth=%d\n",name,d,b);
        /* fill in here */
    
    
        //test
        //printf("Depth = %i\n",d);
        //printf("Path = %s\n", name);
    
    
        struct dir *np =  malloc ( sizeof ( struct dir ));
        
            char npnamebuf[10];
            int x = 0;
            sprintf(npnamebuf, "%d", x);    
    
    
        //printf("NPNAMEBUF = %s\n",npnamebuf);
            np->path=name;
            np->name=npnamebuf;
            np->left=NULL; 
            np->up=*hp;
        np->d=d;
        np->b=b;
    
    
        struct dir *tmp = *hp;
        //struct dir *array[b];
        //tmp->down=&np;
        
        int j;
        for(j=0;j<b;j++){
    
    
        //array[j] = malloc ( sizeof (array[j]) );
        }
    
    
        if (d>0){
        
        int i;
        for(i=0;i<b;++i){
    
    
        if(i<b)
        {
        //create temp pointer
            struct dir *tp = (struct dir *) malloc ( sizeof ( struct dir ));    
            
        char tpnamebuf[10];
        int y = i;
        sprintf(tpnamebuf, "%d", y);
    
    
        tp->path=name;
        tp->name=tpnamebuf;
        tp->left=np;
        tp->up=*hp;
        tp->d=d;
        tp->b=b;
    
    
        //printf("TP NAME %s\n",tp->name); 
        np->right=tp;
    
    
        //printf("RIGHT NODE %s\n", np->right->name);    
    
    
        char path[100];
        sprintf(path, "%s%s/", np->path, np->name);
    
    
        printf("mkdir %s%s/\n", np->path, np->name);
    
    
        create_dir_tree(&np,path,d-1,b);    
        
        //array[i-1]=np;
        //tmp->down=&np;
    
    
        np=tp;
    
    
        }else if(i==b){
    
    
        np->right=NULL;
    
    
        //array[i-1]=np;
        //tmp->down=&np;
    
    
        char path[100];
        sprintf(path, "%s%s/", np->path, np->name);
    
    
        printf("mkdir %s%s/\n", np->path, np->name);
    
    
        create_dir_tree(&np,path,d-1,b);
        
        }
    
    
    
    
        }//endfor
    
    
        }else{
        //printf("DONE\n");
        return;
        }
    
    
    }
    I need help implementing struct dir **down. I left my attempts in there commented out, am I at least going in the right direction or completely off target?

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,553
    Why do you have it declared as a double pointer (two *'s)? A single pointer should be fine, it would point to the first child. You would access the other children through the sibling nodes of the first child.

    I would suggest you use the above method, but if you don't, note that your attempts to assign tmp->down = &np; are incorrect. np is a local variable, so it's address is on the stack, but once the create_dir_tree function returns, the address no longer points to valid memory, or points to memory with different data in there. That results in undefined behavior.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. size of struct with pointers and function pointers
    By sdsjohnny in forum C Programming
    Replies: 3
    Last Post: 07-02-2010, 05:19 AM
  2. Storing function pointers in generic pointers
    By Boxknife in forum C Programming
    Replies: 6
    Last Post: 08-01-2009, 01:33 PM
  3. Pointers to objects -- passing and returning pointers
    By 1veedo in forum C++ Programming
    Replies: 4
    Last Post: 04-04-2008, 11:42 AM
  4. weak pointers and use_count smart pointers
    By Mario F. in forum C++ Programming
    Replies: 2
    Last Post: 07-29-2006, 07:54 AM
  5. pointers to constants and constant pointers
    By homeyg in forum C++ Programming
    Replies: 1
    Last Post: 06-18-2005, 12:02 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21