Thread: double astrix pointer question..

  1. #61
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i understand that dp points to sp
    dp==>sp==>node
    what exactly this expression means
    *dp == sp

    *dp is the derefencing form
    so *dp is the address of pointer sp

    correct?

  2. #62
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,653
    Let's fill these expressions and things with values.
    node = 10
    sp = 20
    dp = 30

    So, that means:
    dp = address of sp (30)
    *dp = value of sp [address of node] (20)
    **dp = value of node (10)
    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.

  3. #63
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    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;
    }
    so what this function does is building some pointer to node variable called elt.
    then elt->next get to be pointed at the pointer which "p" points to
    elt->value gets the "num"
    and then the pointer which "p" pointed to ,points at the elt pointer??
    but its not write
    because by your definition p pints to a pointer
    but *p must point a node
    but it points at a pointer
    so by this operation
    Code:
    *p=elt;
    they transform p from being pointer to pointer to node
    into pointer to pointer to pointer to node

    correct?
    Last edited by transgalactic2; 01-18-2009 at 10:49 AM.

  4. #64
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,653
    Half of the time, I don't even know what you're talking about...
    Code:
    typedef struct node
    {
        int value;
        struct node* next;
    } Node;
    
    int main()
    {
        Node* pFirstNode = malloc( sizeof(Node) );
        pFirstNode->value = 0;
        pFirstNode->next = NULL;
        what1(&pFirstNode, 10);
    }
    
    void what1(Node** p, int num)
    {
       Node* elt; // Define a new empty pointer
       elt = (Node*)malloc( sizeof(Node) ); // Allocate a new node, store in elt
       elt->next = *p; // Put the address stored in pFirstNode in main inside elt->next
       elt->value = num; // Store num in elt->value
       *p = elt; // Assign the address of the new node to pFirstNode in main.
    }
    elt = new node
    p -> pFirstNode -> Node

    Freeing left out for simplicity.
    Now let's see you figure out how or why.
    Last edited by Elysia; 01-18-2009 at 10:53 AM.
    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.

  5. #65
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    Quote Originally Posted by transgalactic2 View Post
    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;
    }
    so what this function does is building some pointer to node variable called elt.
    then elt->next get to be pointed at the pointer which "p" points to
    elt->value gets the "num"
    and then the pointer which "p" pointed to ,points at the elt pointer??
    but its not write
    because by your definition p pints to a pointer
    but *p must point a node
    but it points at a pointer
    so by this operation
    Code:
    *p=elt;
    they transform p from being pointer to pointer to node
    into pointer to pointer to pointer to node

    correct?
    can you tell me in what point i got this theory wrong

  6. #66
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,653
    As I said, half of the time, I don't know what you are writing.

    Quote Originally Posted by transgalactic2 View Post
    so what this function does is building some pointer to node variable called elt.
    then elt->next get to be pointed at the pointer which "p" points to
    elt->value gets the "num"
    and then the pointer which "p" pointed to ,points at the elt pointer??
    That part is correct from what I can assume, if I understand correctly.

    but its not write
    The word is "right", not "write".
    And yes, it is right.

    because by your definition p pints to a pointer
    but *p must point a node
    p points to a pointer, yes, and no, *p must not point to a node. p points to a pointer, Node*, so *p is a Node*.

    but it points at a pointer
    so by this operation
    Code:
    *p=elt;
    they transform p from being pointer to pointer to node
    into pointer to pointer to pointer to node

    correct?
    Absolutely not. What makes you think that?
    "p" is not transformed at all.
    What p is pointing to is assigned the address of the new node which elt points at.

    Have you actually considered that C may not be for you?
    You seem to have an awful lot of problems, never able to do it right, even the simplest of things.
    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.

  7. #67
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by transgalactic2 View Post
    can you tell me in what point i got this theory wrong
    but its not write
    because by your definition p pints to a pointer
    but *p must point a node
    but it points at a pointer

    so by this operation
    Code:

    *p=elt;

    they transform p from being pointer to pointer to node
    into pointer to pointer to pointer to node
    The heart of your misunderstanding is in red. By assigning a pointer the same value as another pointer, they point to the same place:
    Code:
    node *p, *elt=malloc(sizeof(node));
    p=elt;
    elt->next=p;
    Would be a circular linked list with two members. You can swap pointers around as much as you like:
    Code:
    elt=p;
    p->next=elt->next;
    elt->next=p->next;
    p=elt;
    p=p->next;
    They all still point to a node, not to a pointer to a pointer to a pointer to a node or something.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #68
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    the pointer p the "head" points to *p which points to a node
    correct?

    but when we do
    Code:
    *p=elt;
    p is still the head ,but now p points to elt (the new *p)
    correct?

  9. #69
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    but in this case
    Code:
    node **p, *elt=malloc(sizeof(node));
    *p=elt;
    will mean that p will point to elp

    because p points to *p so if our new *p is elp
    then p points to elp
    correct?

  10. #70
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,401
    No, that results in undefined behaviour. p does not point to anything, so *p is a pointer that does not exist. Other than that, yes, p points to elt.
    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

  11. #71
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i thought that when we define
    Node **p;
    that means that p points to another pointer (*p) and this (*p) points to a node
    where is my mistake?

  12. #72
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Okay, you are wanting to use pointers to pointers, so I guess in this case:
    Code:
    node **p, *elt;
    *p=elt;
    p is a pointer to a pointer, however it cannot be a pointer to a node, so you cannot access the members of a node using *p (there is no *p->next). However, if you cast it as a character block, (char*)*p you can accesses the contents of the node pointed to by the pointer *p points to byte by byte.

    I haven't seen pointers to pointers performing a useful purpose in linked lists tho, so you are kind of pouring pickled pepper lemon juice in your milk here. I could be wrong.
    Last edited by MK27; 01-18-2009 at 01:27 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  13. #73
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,401
    Quote Originally Posted by MK27
    p is a pointer to a pointer, however it cannot be a pointer to a node, so you cannot access the members of a node using *p (there is no *p->next).
    It is correct to say that since p is a pointer to a pointer it cannot be a pointer to a node, since a node is not a pointer. However, it is not correct to say that one cannot access the members of a node via p, since *p is a pointer to a node, hence (*p)->next is valid (assuming that p actually points to an existing pointer to a node, rather than to nothing as is the case at the moment).
    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. #74
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by laserlight View Post
    (*p)->next is valid (assuming that p actually points to an existing pointer to a node, rather than to nothing as is the case at the moment).
    Okay. Maybe the reason I haven't tried that is because this:
    Code:
    node *test=malloc(sizeof(node)), **p;
    *p=test;
    Actually causes a segfault on my system. I'm guessing that transgalactic2 is not actually trying any code to see what it does before (s)he asks what it could mean.

    I can do this:
    Code:
    node *test=malloc(sizeof(node)), **p;
    p=&test;
    which I imagine is not the same thing, but then (*p)->next causes a segfault.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  15. #75
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    so you agree with me that if we define
    Node **p;
    then we have two pointers in this definition
    in which
    p points *p

    correct?

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