Thread: double astrix pointer question..

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by transgalactic2 View Post
    so **p point to a Node * type variable
    Of course not.
    p points to a Node*!
    Pointers to pointers are not special!
    So dereference your pointer and you get the original Node* variable.
    But wait, you can dereference that original variable too, so you get the Node value (or data).

    Code:
    int* x = malloc( sizeof(int) );
    *x = 100;
    int** y = &x;
    printf("%i", **y);
    printf("%i", *x);
    // y = address of x (pointer to x!)
    // *y = x (the address in x!)
    // **y == 100 (the value pointed to by x!)
    free(*y);
    *y = malloc( sizeof(int) );
    **y = 200;
    printf("%i", **y);
    printf("%i", *x);
    free(x);
    How does this code work and what does it print, do you think?
    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.

  2. #17
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    Quote Originally Posted by laserlight View Post
    Okay, at least you got the first part. Now, what is the type of elt->next? What is the type of *p?

    *p is the value of P

    elt->next is type node

  3. #18
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by transgalactic2 View Post
    *p is the value of P
    Huh?

    elt->next is type node
    That is incorrect. Read the source again.
    Last edited by Elysia; 12-29-2008 at 07:24 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.

  4. #19
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i ment the value of p
    *p is the value of variable p

  5. #20
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes. Yes, it is. But what type is *p?
    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. #21
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    Node *

    I got a linked list i dont know what points to what
    ??

  7. #22
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well then, if you understand how the p2p works, then you should be able to understand the code. Is there a specific part you fail to understand yet?
    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.

  8. #23
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    In this code
    I pointed in a comment.
    it all comes down to who is the "next" of who
    and i cant undertand it here?

    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;
    }

  9. #24
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It assigns *p to it, and what is *p?
    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. #25
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    *p is of type Node*

    but it doesnt tell me who is the next object of who??

  11. #26
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, it does. This line says is all:
    *p=elt;
    Besides, it is a linked list and at a glance, it should be obvious what it does.
    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.

  12. #27
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    if it were p=elt
    i would say it puts the address of elt and puts it to p

    but its *p?

  13. #28
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i could say
    i would say it puts the address of elt and puts it to *p

    but it meaningless to me
    because its a linked list
    i cant see what points to what

  14. #29
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The key to the linked list is on this line:
    Code:
    elt->next=*p;
    This says that the node pointed to by elt now has a next pointer that points to the node pointed to by *p. In more graphical terms, this tongue twister is like this:

    First, we have two pointers that point to nodes:
    Code:
    *p --> [node #1]
    elt --> [node #2]
    Now, we get elt's next pointer to point to what *p is pointing to:
    Code:
    elt->next --> [node #1]
    This means that we now have two nodes linked together:
    Code:
    [node #2] --> [node #1]
    Now, here's the catch. elt points to node #2, but elt itself is a local variable. When the function ends, elt will be destroyed. What to do?

    We can choose to return elt, but in this case we choose to make *p point to what elt points to. As such, the caller now has a pointer to node #2.
    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

  15. #30
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by transgalactic2 View Post
    if it were p=elt
    i would say it puts the address of elt and puts it to p

    but its *p?
    That's because they are of the same type, both elt and *p are pointers to type Node that's why they can be assigned.
    You can't equate p = elt because they are of different types; p is a pointer to another pointer while elt is a pointer to type Node.
    Last edited by itCbitC; 12-29-2008 at 09:18 AM.

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