whats th difference between *ptr and **ptr in a signature..

This is a discussion on whats th difference between *ptr and **ptr in a signature.. within the C Programming forums, part of the General Programming Boards category; when i got in my function signature node *ptr it takes the pointer as a parameter. what happens if its ...

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

    whats th difference between *ptr and **ptr in a signature..

    when i got in my function signature node *ptr it takes the pointer as a parameter.

    what happens if its node **ptr

    i know that node* is a type of variable in a funtion
    it says "take a node type variable called ptr"

    but when i do node **ptr
    by my logic it says "take a node type variable called *ptr"


    i was told that in the node *ptr we cant change the parameter
    but int the ** we can

    and i cant see how it goes in the memory
    whats the difference?
    Last edited by transgalactic2; 12-14-2008 at 03:00 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The difference is that "take a node type variable called *ptr" makes no sense. (Edit: That is to say, * is a syntactic element, not a character that can appear in a variable name.)

    node *ptr is a pointer. If you follow the pointer, you will find a node.

    node **ptr is a pointer. If you follow the pointer, you will find another pointer. If you follow that pointer, you will find a node.
    Last edited by tabstop; 12-14-2008 at 03:04 PM.

  3. #3
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    so it goes to the pointer which ptr points to.
    what about the stuff about the ability to change the input parameter with **

    is it possible?

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    *ptr and **ptr are both pointers, the only difference between them is that the former has one level while the latter has two levels of indirection. You can change the input parameter with either of them since *ptr as well as **ptr are both lvalues. The exception would be if they pointed to string literals.

  5. #5
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    what if u made ***ptr it will be a 3rd level pointer?

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by lolguy View Post
    what if u made ***ptr it will be a 3rd level pointer?
    yes that would be three levels of indirection; why though would you want so many levels of indirection?

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by itCbitC View Post
    yes that would be three levels of indirection; why though would you want so many levels of indirection?
    Because you want to set a pointer to a pointer to a new value within a function, perhaps? [I think I've used *** about three times in my life - and I've never used **** ever].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    That exactly was my point; three levels of indirection used sparsely if at all.

  9. #9
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    One is a pointer, (*ptr) and the other is a pointer to a pointer (**ptr).
    You can go on forever eg. ***************************ptr

  10. #10
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    yah i think 4 is like MAX for *

  11. #11
    Registered User
    Join Date
    Dec 2008
    Posts
    15
    But, would you only use **ptr when dereferencing?

    I ask because I *think* I've seen it used as an argument in a function prototype (do you guys call them prototypes or declarations here?)

    I've confused myself with these double asterisks myself... Could anyone give an example of when you would want a pointer to a pointer? Why wouldn't you just change where the first pointer pointed to?

    -Dave

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by mutandis View Post
    But, would you only use **ptr when dereferencing?

    I ask because I *think* I've seen it used as an argument in a function prototype (do you guys call them prototypes or declarations here?)

    I've confused myself with these double asterisks myself... Could anyone give an example of when you would want a pointer to a pointer? Why wouldn't you just change where the first pointer pointed to?

    -Dave
    Well, for the obvious reason that you can't change where the first pointer points to, just like you can't change any parameter passed in to a function. If you search the board, you will find 374 examples of people trying to create a new linked list like so:
    Code:
    void create_list(Node *head, Data start_data) {
        head = malloc(sizeof Node);
        head.data = start_data;
        head.next = NULL;
    }
    and wondering why they didn't have a list when they got back to main.

  13. #13
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by mutandis View Post
    But, would you only use **ptr when dereferencing?

    I ask because I *think* I've seen it used as an argument in a function prototype (do you guys call them prototypes or declarations here?)

    I've confused myself with these double asterisks myself... Could anyone give an example of when you would want a pointer to a pointer? Why wouldn't you just change where the first pointer pointed to?

    -Dave
    Code:
    // this might be a bit 'old style' C
    
    main( int argv, char **argc) {
    
    .....etc....

    eg programname parameter1 parameter2 etc....eg print file1 file2 etc...

    argv is the number of parameters to a program
    **argc are the parameters, a pointer to an array of strings, or expressed in another
    way a pointer to a pointer to a character.

    Code:
    in print file1 file2 
    argc[0]="file1"
    argc[0][1]='i'
    argc[0][4]='1'
    
    argc[1]="file2"
    argc[1][0]='f'
    argc[1][4]='2'
    
    
    char words[2][6];
    ptr=words;
    strcpy(*ptr,"word1");
    ptr++;
    strcpy(*ptr,"word2");
    
    If you printed
    printf("%s\n",words[0]);
    printf("%s\n",words[1]);
    
    it would probably say
    
    word1
    word2
    
    
    or
    ptr=words;
    printf("%s\n",*ptr);
    ptr++
    printf("%s\n",*ptr);
    
    Should do the same.
    Then again it might fall over
    Last edited by esbo; 12-14-2008 at 08:46 PM.

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,784
    Once again, esbo shows how little esbo really knows about C.
    You cannot and should not assign a 2D-array (char [x][y]) to a pointer-to-pointer (p2p, char**).
    But it's quite simple.

    A pointer is typically used to modify a variable that it found outside the function that modifies it (or to avoid copying of data).
    So if we create a linked list, we need to store its address in a pointer, right? So then main, for example, passes in the address of the pointer we want to assign it to, so we do:

    Code:
    void foo(T** ptr);
    
    int main()
    {
        T* ptr;
        foo(&ptr);
    }
    
    void foo(T** ptr)
    {
        *ptr = something;
    }
    See that? If you read from the right, first you see the variable name. Then you see the first *, saying it's a pointer. And to the left of that is the type it points to.
    So ptr here pointers to a T*, or in other words, it points to a pointer.

    Makes sense, yes? When you think of that the * binds to the type and not the name, it all makes sense.
    http://www.research.att.com/~bs/bs_faq2.html#whitespace

    And esbo, main returns int, not nothing. Plus globals variables are evil. Not to mention your (probably) faulty assignment of char[2][6] to ptr (which, again, we cannot see because you insisted on making it global), whose type is probably char**.
    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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21