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

    Join Date
    Jul 2013
    Posts
    109
    Rep Power
    3

    Need help with conversion of a pointer


    I need help understanding an expression(and the precedence of the compiler's actions when evaluating the expression), involving conversion of a pointer.

    My declarations are such:
    Code:
    struct Book /*a simple struct*/
    {
        char *name;
        char *writer;
        char *publisher;
        int year;
        long ID;
        float cost;
    };
    
    Book *array[5]; /*an array of pointers, each pointing to a Book struct*/ 
    
    "pbook": is any member of the array above, i use it as function arguments by-name-- pbook1=array[0], pbook2=array[1] etc'
    
    typedef Book *BookPtr; /*BookPtr is a pointer to a Book struct*/
    
    (*ppbook)----> is a pointer to (*pbook), so it's actually a double pointer 
    
    The expression in question:
    
    BookPtr pbook1= *(BookPtr *)ppbook1;
    BookPtr pbook2= *(BookPtr *)ppbook2;
    Why is the "*" operand in the inner brackets located after the word "BookPtr" and not before as such: *(*BookPtr)

    But most importantly- what does it mean when the "*" is ALSO LOCATED OUTSIDE THE BRACKETS? in this case, is it true that the conversion takes place first and then the "*" is applied? And when it IS applied what does it mean about the whole expression?
    Last edited by C learner; August 2nd, 2013 at 04:02 AM. Reason: Conversion of a pointer
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    40
    Rep Power
    19
    Code:
    BookPtr pbook1= *(BookPtr *)ppbook1;
    Why is the "*" operand in the inner brackets located after the word "BookPtr" and not before as such: *(*BookPtr*)
    Because it is not correct.

    After
    Code:
    typedef Book * BookPtr;
    You can use 'BookPtr' as a pointer;

    Book *array[5]; is same as BookPtr array[5];


    Some references:

    Pointers - C++ Documents
    http://www.cplusplus.com/doc/tutorial/pointers/

    Tutorial: Pointers in C and C++
    http://www.augustcouncil.com/~tgibson/tutorial/ptr.html

IMN logo majestic logo threadwatch logo seochat tools logo