Thread: double astrix pointer question..

  1. #31
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by transgalactic2 View Post
    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
    perhaps showing the function that calls what1() might clarify things

  2. #32
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    the link list structure is like that
    (data|next)==>(data|next)==>(data|next)==>(data|ne xt) etc..

    the is some node called elt its structure is:
    (data|next)
    if they would say
    Code:
    elt->next=p;
    so the next of "elt" is node p
    (data|next)==>(data|next)
    but they say

    Code:
    elt->next=*p;
    so its
    (data|next)==>(data)

    ??

    i need an explanation that explains it in this simple way of schematics:
    (data|next)==>(data|next)==>(data|next)==>(data|ne xt) etc..
    Last edited by transgalactic2; 01-17-2009 at 11:09 AM.

  3. #33
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by transgalactic2 View Post
    the link list structure is like that
    (data|next)==>(data|next)==>(data|next)==>(data|ne xt) etc..

    the is some node called elt its structure is:
    (data|next)
    if they would say
    Code:
    elt->next=p;
    so the next of "elt" is node p
    (data|next)==>(data|next)
    but they say

    Code:
    elt->next=*p;
    so its
    (data|next)==>(data)

    ??

    i need an explanation that explains it in this simple way of schematics:
    (data|next)==>(data|next)==>(data|next)==>(data|ne xt) etc..
    So what part in this post don't you understand?

  4. #34
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i dont understand how whould it change the schematics of the linked list if its
    *p instead of p
    ??

  5. #35
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Hello! Wake up!
    Because it wants to store the address of the Node, not the address of the pointer to points to the Node!
    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. #36
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    can you please make a schematics explaining this
    because things like
    "address of the pointer to points to the Node" are very abstract
    i cant imagine it.
    can you say how would my linked list look like
    after this code??

  7. #37
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It's not very abstract.
    Code:
    int main()
    {
        int* p = malloc( sizeof(int) );
        foo(&p);
    }
    
    void foo(int** p)
    {
        free(*p);
    }
    First, malloc is called and the address where the allocation was made is stored in p.
    Then the address of p (the address of the pointer!) is passed to foo, which then dereferences that pointer-to-pointer-to-int to get the address stored in p in main, and frees it.

    Do you see the connection here?
    p in foo is the address of the pointer to the int.
    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. #38
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i understand every thing till this part
    "which then dereferences that pointer-to-pointer-to-int to get the address stored in p in main, and frees it."

    whats dereferces?? (i know that there is no reference in C?)
    and i dont know what that means "pointer-to-pointer-to-int to get the address stored in p in main"
    can you tell that in simpler words?

  9. #39
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Dereference is the process called when you put the * before a pointer.
    int* p;
    *p = 5; // Dereferences p

    >>pointer-to-pointer-to-int
    int** basically. It is a pointer, which contains the address of another pointer (thus points to that pointer), and that pointer contains the address of the allocated int, it points to the int:
    Code:
    Pointer1:
        -> points to Pointer2
            -> Pointer2:
                -> points to allocated int
    >>to get the address stored in p in main
    Basically, what that means is that we get the value stored inside the pointer named p, which is defined inside main.
    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. #40
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    Code:
    *p = 5; // Dereferences p
    so derefences means that you put a value in the address of p
    correct?
    and int** means to use the next pointer of the linked list.

    and in this part
    Code:
    foo(&p);
    you take a number only a number and put it as a parameter of a function
    its like saying "use 5 as a pointer"
    its just a number
    you need to put there a name of a pointer
    ??

  11. #41
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by transgalactic2
    so derefences means that you put a value in the address of p
    correct?
    No, to dereference a pointer is to get what the pointer points to. In the example you get what the pointer points to and assign the value 5 to it.

    Quote Originally Posted by transgalactic2
    you take a number only a number and put it as a parameter of a function
    its like saying "use 5 as a pointer"
    No, you take the address of the object, and pass it to the function named foo as an argument.
    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. #42
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    what we do with this passed address?
    where does it go??

  13. #43
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by transgalactic2
    what we do with this passed address?
    where does it go??
    It is copied to the function's argument, which is a pointer. After all, a pointer contains an address.
    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. #44
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No, no, no. Are you living under a rock?
    Have you really no book or do you lack so much understanding of the language?

    Quote Originally Posted by transgalactic2 View Post
    Code:
    *p = 5; // Dereferences p
    so derefences means that you put a value in the address of p
    correct?
    No, no, no.
    Dereferencing means getting the value the pointer points at.
    So the "*p" part is described as "dereference the pointer p."
    Assigning a value is a separate step. Naturally, to assign a value where the pointer points, you must first dereference the pointer.

    Look at it this way:
    Every variable in a program has to be stored somewhere. So it is stored in memory. And to be able to access things in memory, everything must have an address.
    Variables are automatically handled by the compiler, so you can set and get a value from them without trouble. It's transparent.
    You can also figure out the address where variables are stored in memory. This "value" must be stored in a pointer.
    A pointer is also a variable.
    If you assign something to a pointer, you assign an address, because that's what pointers store.
    You can also dereference a pointer, which basically means, get (or set) that value stored at the address stored inside this pointer.
    Does that make sense?

    and int** means to use the next pointer of the linked list.
    int** means it's a pointer that stores addresses of int* pointers (pointers-to-int).

    and in this part
    Code:
    foo(&p);
    you take a number only a number and put it as a parameter of a function
    its like saying "use 5 as a pointer"
    its just a number
    you need to put there a name of a pointer
    ??
    This part takes the address of the local variable p, whose type is int*, and passes it along to the function foo, which expects the address of an int* variable (thus it takes an int** argument).
    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.

  15. #45
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by transgalactic2 View Post
    what we do with this passed address?
    How about this then?

    Code:
    int main()
    {
        int* p;
        Allocate(&p);
        Destroy(&p);
    }
    
    void Allocate(int** p)
    {
        *p = malloc( sizeof(int) );
    }
    
    void Destroy(int** p)
    {
        free(*p);
        *p = NULL;
    }
    If you can understand how that code works and why it needs to take pointer-to-pointers-to-int, then you have solved the mystery of your original code.

    PS: Feel free to merge these posts if that is your wish. Yes, I'm talking to YOU, Moderators!
    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.

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