Thread: double astrix pointer question..

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    double astrix pointer question..

    what this function does?

    Double astrix says that the parameter is the pointer which P points at
    so the parameters is p->next and num.
    They create a node called elt ,they assign it a memory space in a size node
    and make it a node pointer.
    next they say that elt->next=p->next
    elt->value is num
    and p->next=elt


    I cant imagine what is the structure of the combination of those to marked red lines.
    ??
    Code:
    typedef struct node {
        int value;
        struct node *next;
    }Node;
    
    void what1(Node ** p,int num){
       Node *elt;
       elt=(Node*)malloc(sizeof(Node));
       elt->next=*p;
       elt->value=num;
       *p=elt;
    }

  2. #2
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    pass by reference anyone?
    Just GET it OFF out my mind!!

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by transgalactic2 View Post
    what this function does?

    Double astrix says that the parameter is the pointer which P points at
    so the parameters is p->next and num.
    The parameter is a pointer that points to the pointer which points to a Node. The ** means a pointer to a pointer.
    So the parameter is p and num.
    There is also a pointer which p points at. Lets call it nodePtr. There is also a node in which nodePtr points to. Lets call it node.

    You have this:
    p -> nodePtr -> node

    So:
    elt-> next = *p means: elt->next = nodePtr
    *p = elt means: nodePtr = elt

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by audinue View Post
    pass by reference anyone?
    In C?

    What I think you want is
    Code:
    (*p)->next
    If I understand you correctly.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It basically takes the address of a pointer to which it assign malloced memory. Only a pointer can store an address, so there you go.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    I think EVOEx is on the mark if I understand your question. p is a pointer to pointer to a Node while *p is a pointer to a Node object as in
    Code:
    elt->next=(*p)->next 
    (*p)->next=elt

  7. #7
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    what means Node **p in the signature??

    p->next ?(a pointer which points to a pointer)

    am i correct?

  8. #8
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    p isn't the same as p->next if that is what you mean. I answered what p means in post #3.

    It is indeed a pointer that point to a pointer. So its value (*p) is a pointer that points to a node. So p points to a pointer which pointer points to a node.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by transgalactic2 View Post
    what means Node **p in the signature??

    p->next ?(a pointer which points to a pointer)

    am i correct?
    Node** means it is a pointer to a variable of type Node*. It can be no more simple than that.
    And the "p->next" syntax just means "(*p).next", and since *p is a Node*, it is illegal.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    so **p point to a Node * type variable

    what point to what during this function (in every step)

    ??

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by transgalactic2
    so **p point to a Node * type variable
    No, if p is a Node**, then **p is a Node.
    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

  12. #12
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    but what is the stucture of **p regarding a linked list system.

    I cant imagine what points to what in every step during the running of this function.
    Code:
    typedef struct node {
        int value;
        struct node *next;
    }Node;
    
    void what1(Node ** p,int num){
       Node *elt;
       elt=(Node*)malloc(sizeof(Node));
       elt->next=*p;
       elt->value=num;
       *p=elt;
    }

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by transgalactic2
    I cant imagine what points to what in every step during the running of this function.
    What exactly is the part that you cannot understand?

    Look, you have to try and explain what the function does. If you cannot fully explain it, at least show some effort by explaining these two lines:
    Code:
    Node *elt;
    elt=(Node*)malloc(sizeof(Node));
    Then try to explain what this line does:
    Code:
    elt->next=*p;
    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

  14. #14
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    I dont know what it does.
    I was asked to explain what it does.
    i can guess but this doble astrix thing and linked list
    i dont know whats happening in the linked list when the double astrix involved.

    Code:
    typedef struct node {
        int value;
        struct node *next;
    }Node;
    
    void what1(Node ** p,int num){
       Node *elt;
       elt=(Node*)malloc(sizeof(Node));  //create a node called elt ,assign it some memory
       elt->next=*p;     //from this point i dont know what the effect of the double astrix
       elt->value=num;
       *p=elt;
    }

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Okay, at least you got the first part. Now, what is the type of elt->next? What is the type of *p?
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about pointer arithmetic and types
    By brooksbp in forum C Programming
    Replies: 4
    Last Post: 08-22-2008, 01:53 PM
  2. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  3. Unknown Math Issues.
    By Sir Andus in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2006, 06:54 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM