Thread: Is this correct for assigning/dereferencing double ptr in struct?

  1. #1
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    266

    Is this correct for assigning/dereferencing double ptr in struct?

    Say I have the following:


    Code:
    struct node{
        node **somenode;
        node *othernode
    }foo;
    
    void somefunc(struct node *foo){
    
    }

    How would I dereference somenode? Also, how could I point it to othernode?

    My compiler kept yielding warnings with some things I tried and so I've come to find that this works. But is it the correct way? Out putting addresses showed that somenode referenced what foo->othernode referenced instead of referencing foo->othernode itself.

    Code:
    void somefunc(struct node *foo){
    
         foo->somenode = &*(foo->othernode);
    }

    Simply doing:

    Code:
    void somefunc(struct node *foo){
    
         foo->somenode = &foo->othernode;
    }
    gives me warnings.
    Last edited by Syscal; 09-29-2010 at 10:18 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It looks like that should be:
    Code:
    struct node{
        struct node **somenode;
        struct node *othernode;
    };
    
    void somefunc(struct node *foo) {
        *foo->somenode = foo->othernode;
    }
    Assuming that foo->somenode already points to a struct node pointer. Why do you want the somenode member, anyway?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    266
    No particular reason, I just was curious as to how it worked. I thought I needed it at one point but turned out I didn't. It was a stack implementation and I was going to use a double pointer member as the stack pointer. Like I said, I didn't need it so I decided not to use it.

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Simply doing:

    Code:
    void somefunc(struct node *foo){
    
         foo->somenode = &foo->othernode;
    }
    What warning? Why not post it?
    Btw,

    Code:
    struct node{
        node **somenode;
        node *othernode
    }foo;
    Here foo is a global variable of type struct node. I'm not sure you really mean it?
    Edit: If you are using C, I guess you have typedef struct node node; before your struct node definition?
    Last edited by Bayint Naung; 09-29-2010 at 10:58 PM.

  5. #5
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    266
    Edit: nevermind that code...

    This should give you an idea of what I'm talking about;


    the structures(not global):

    Code:
    struct stack_node_t{
        struct stack_node_t *next;
        void *data;
    };
    
    struct stack_t{
        struct stack_node_t **stack_ptr;
        struct stack_node_t *root;
        int size;
        int max;
    };

    Code:
    void push(struct stack_t *stack, void *data){
        if(stack->size < stack->max){
            if(!stack->size){
                stack->root = malloc(sizeof(struct stack_node_t));
                stack->root->next = NULL;
                stack->root->data = data;
            }
            else{
                (*stack->stack_ptr)->next = malloc(sizeof(struct stack_node_t));
                stack->stack_ptr = &(*stack->stack_ptr)->next;
                (*stack->stack_ptr)->next = NULL;
                (*stack->stack_ptr)->data = data;
            }
            stack->size++;
        }
        else{
            fputs("ERROR: Stack overflow\n", stdout);
        }
    }
    Last edited by Syscal; 09-30-2010 at 12:20 AM.

  6. #6
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Complicated for a stack!!!
    What's the point of struct stack_node_t **stack_ptr; ??

  7. #7
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    266
    haha, I know, thats why I opted not to use it. I just would like to know for future reference how these double pointer members ought be dereferenced. The idea was that it points to the top of the stack. I realize I dont need it though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-03-2008, 03:10 AM
  2. Compiling c++ intrinsics commands
    By h3ro in forum C++ Programming
    Replies: 37
    Last Post: 07-13-2008, 05:04 AM
  3. Help calling function is asm
    By brietje698 in forum C++ Programming
    Replies: 24
    Last Post: 12-06-2007, 04:48 PM
  4. Unknown Math Issues.
    By Sir Andus in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2006, 06:54 PM
  5. Help with multi function progam
    By WackoWolf in forum C Programming
    Replies: 22
    Last Post: 10-13-2005, 02:56 AM