#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    109
    Rep Power
    2

    Declaration of pointer to pointer ??


    I have an array of pointers, each one pointing to a struct called Book:

    Code:
    Book *array[5];  /*The array of pointers*/
    
    struct Book
    {
        char *name;
        char *writer;
        char *publisher;
        int   year;
        long ID;
        float cost;
    };
    And i'm using a SWAP function that swaps the order of two pointers, each of them pointing to another pointer which points to a book.

    Code:
    typedef Book *BookPtr; /*BookPtr is now of type: pointer to Book struct*/
    
    void swap(BookPtr *ppbook1, BookPtr *ppbook2)
    {
        BookPtr temp= *ppbook1;
        *ppbook1=*ppbook2;
        *ppbook=temp;
    }
    Inside the swap function i use a POINTER TO A POINTER TO A BOOK: *ppbook-->*BookPtr--->Book because otherwise only the values are swapped and nothing happens.


    My question is: In the book i'm learning from they declare the "temp" from the swap function to be a "BookPtr" type !
    But BookPtr is a pointer to struct, when it actually should be A POINTER TO A POINTER TO STRUCT ??

    Shouldn't they declare(?):
    Code:
    typedef BookPtr *pp;   
    
    AND THEN:   pp temp= *ppbook1;
    I mean so "temp" would be declard to store a pointer to a pointer to Book, instead of just pointer to Book ?
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    Think though the levels of indirection methodically.

    You have an array of Book pointers. You want to be able to swap pointers in that array, right? So when you pass a pointer to one of those pointers (eg, you'd pass &array[3] to swap), you'd have a pointer to a pointer. That means that the parameters of that function would have to be pointers to pointers, or Book**. Within the function, you're swapping pointers to Book, Book*, which you accomplish with the Book** parameters by dereferencing them, removing one level of indirection.

    All you're doing with that typedef is to apply some syntactic sugar that hides one level of indirection. The function looks right to me.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    109
    Rep Power
    2
    Wait, so temp is declard to hold the data WHICH IS POINTED BY "ppbook1" ? (the data=which is a regular pointer)

    I think i see what you mean :cheers:
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    I would like to recommend an article to help you think about pointers and levels of indirection. Twenty years ago The C Users Journal ran a two-part article by Christopher Skelly, "Pointer Power in C and C++". It's reposted at:
    http://collaboration.cmc.ec.gc.ca/sc...lly/skelly.htm (Part 1) and at http://collaboration.cmc.ec.gc.ca/sc...lly/skelly.htm (Part 2).

    Or you could Google on "Pointer Power in C and C++" "Christopher Skelly" for other links to it.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    109
    Rep Power
    2
    Thank you!

    I'm going to read it.

IMN logo majestic logo threadwatch logo seochat tools logo