Thread: How to access members to double pointer struct?

  1. #1
    Registered User
    Join Date
    Jan 2020
    Posts
    8

    How to access members to double pointer struct?

    I'm creating a *Node inside a function (allocate and accessing its members) but when I try to access compiler jumps.

    l've tried this (Node*)(*_node)->item = ""; but it says "suspicious pointer conversion".

    Thanks in advance.

    Code:
    int main (int argc, char* argv[]) {
       Node *node;   
       // allocate new node
       initialize(&node);
       
       // print node
       if (!strlen(_node->item) )
          printf("[null, ");
       else
          printf("[%s, ", _node->item);
    
       if (!_node->next)
          printf("null]\n");
       else
          printf("*node]\n");
    
       return 0;
    }
    void initialize(Node **_node) {
       *_node = malloc( sizeof(Node));
       //how to l access to _node members???
       // _node->item = ""
       // _node->next = NULL;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,835
    What you want to do is something like this:
    Code:
    void initialize(Node **_node) {
        *_node = malloc(sizeof(**_node));
        if (*_node) {
            (*_node)->item = "";
            (*_node)->next = NULL;
        }
    }
    But I suggest using another variable to make things simpler:
    Code:
    void initialize(Node **_node) {
        Node *node = malloc(sizeof(*node));
        if (node) {
            node->item = "";
            node->next = NULL;
        }
        *_node = node;
    }
    Be wary about setting node->item to be an empty string literal though: you can only do this safely if the item member is a const char* rather than a char*.
    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
    Registered User
    Join Date
    Jan 2020
    Posts
    8
    @laserlight Hey thank you for the answer that is what I was looking for. Good to have you in this community. Regards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Struct members's access
    By Brian_Teir in forum C Programming
    Replies: 9
    Last Post: 12-06-2019, 01:58 AM
  2. Pointer to struct members and to the whole structure.
    By satsriakal in forum C Programming
    Replies: 15
    Last Post: 12-21-2017, 04:19 PM
  3. Access to struct members
    By j.lang in forum C Programming
    Replies: 1
    Last Post: 01-06-2017, 12:05 PM
  4. How to access members of a struct
    By Bnchs in forum C Programming
    Replies: 9
    Last Post: 03-25-2008, 02:28 PM
  5. Pointer to struct members
    By kybert in forum C Programming
    Replies: 7
    Last Post: 07-01-2003, 08:27 AM

Tags for this Thread