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

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.

  3. #3
    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 09:46 PM.

Popular pages Recent additions subscribe to a feed