Thread: Pointer's address

  1. #1
    Registered User
    Join Date
    Apr 2015
    Posts
    42

    Address' pointer

    Hi,

    I'm working on saving a binary tree into file and reading it which later I learned called as serializing and deserializing of the tree. I found some codes and one of them is this:

    Code:
    void deserialize(BstNode *&root, FILE *fp);
    Which is something I don't understand. Why would we take address of the pointer like that (*&root). Doesn't that equal to just root.

    I always imagined pointer (*) and (&) addresses as level. While * increases it & decreases. So this seems like first going 1 level up and going 1 level down to me. But of course I know the basics of pointers although it seems not so good to understand this code.

    My 2nd thought was *(&root) which is a pointer that points root's address instead of its value. Probably this is what that code means but still I can't understand how it works.

    Here inside that function:

    Code:
        int val;
        if ( !fscanf(fp, "%d ", &val) || val == MARKER)
           return;
    
    
        root = GetNewNode(val);
        deSerialize(root->left, fp);
        deSerialize(root->right, fp);
    FYI when you change *&root with;
    root (you get error),
    *root (0 error or warning but function doesn't work).
    Last edited by Vsky; 01-02-2016 at 06:01 AM.

  2. #2
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    That shouldn't even compile.

    Depending on what deSerialize() actually does, it should be either

    a) void deserialize(BstNode **root, FILE *fp);

    or

    b) void deserialize(BstNode *root, FILE *fp);


    Looking at deSerialize(root->left, fp); I am guessing option (b), but without seeing the actual deSerialize implementation, *shrug*

    Edit: now I'm not so sure about my guess because deSerialize() and deserialize() are different functions :/

  3. #3
    Registered User
    Join Date
    Apr 2015
    Posts
    42
    Quote Originally Posted by Hodor View Post
    That shouldn't even compile.

    Depending on what deSerialize() actually does, it should be either

    a) void deserialize(BstNode **root, FILE *fp);

    or

    b) void deserialize(BstNode *root, FILE *fp);


    Looking at deSerialize(root->left, fp); I am guessing option (b), but without seeing the actual deSerialize implementation, *shrug*

    Edit: now I'm not so sure about my guess because deSerialize() and deserialize() are different functions :/
    Oh no they are not different functions the actual function is deSerialize I just changed it while typing here and forgot to change inside. Also program compiles and works as it should without a problem. I've just simply opened this topic to learn logic behind it. There is no problem with code. I can share rest of the code if you want but that is just irrelevant. There is only GetNewNode function there that you don't see which simply creates new BstNode struct, assigns val to its data then returns it.

    Code:
    BstNode* GetNewNode(int data) {
        BstNode* newNode = new BstNode();
        newNode->data = data;
        newNode->left = newNode->right = NULL;
        return newNode;
    }
    Also this is the struct:

    Code:
    struct BstNode {
        int data;
        BstNode* left;
        BstNode* right;
    };

  4. #4
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    http://codepad.org/ZOHzh7ue

    Code:
    #include <stdio.h>
    
    struct BstNode {
        int data;
        BstNode* left;
        BstNode* right;
    };
    
    void deserialize(BstNode *&root, FILE *fp);
    
    int main(void)
    {
        printf("ok\n");
        return 0;
    }
    
    Output:
    Line 5: error: expected specifier-qualifier-list before 'BstNode'
    Line 9: error: expected ')' before '*' token
    Somehow I don't think you're using C...
    Last edited by Hodor; 01-02-2016 at 07:01 AM.

  5. #5
    Registered User
    Join Date
    Apr 2015
    Posts
    42
    Quote Originally Posted by Hodor View Post
    C code - 15 lines - codepad

    Code:
    #include <stdio.h>
    
    struct BstNode {
        int data;
        BstNode* left;
        BstNode* right;
    };
    
    void deserialize(BstNode *&root, FILE *fp);
    
    int main(void)
    {
        printf("ok\n");
        return 0;
    }
    
    Output:
    Line 5: error: expected specifier-qualifier-list before 'BstNode'
    Line 9: error: expected ')' before '*' token
    Somehow I don't think you're using C...
    Oh damn... I opened topic to wrong place. It's not C, it is C++.

    Edit: Although what makes it work at C++ but doesn't at C ?

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Edit: Although what makes it work at C++ but doesn't at C ?
    C doesn't have references in quite the same manner that C++ does.

    Code:
    void deserialize(BstNode *&root, FILE *fp);
    That is a reference to a BstNode pointer.

  7. #7
    Registered User
    Join Date
    Apr 2015
    Posts
    42
    @whiteflag

    Can you explain that *& reference? And if I compile this as C file and change *& to ** as Hodor said would that work?

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Can you explain that *& reference?
    I thought I did. The variable root is a reference. The type of the reference is a BstNode pointer. I attempted to point this out using color coding before.
    And if I compile this as C file and change *& to ** as Hodor said would that work?
    Well, no. It would be a conscious effort to make that change, because you need to be consistent, and because pointers and references have different properties. In general, references cannot be reassigned once they are defined. They also do not need to be explicitly dereferenced, allocated, or released like pointers do.

    Even if you made the change, new is not a C operator either.

    So, changing from C++ to C is not really a good choice.

  9. #9
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by whiteflags View Post
    Even if you made the change, new is not a C operator either.
    Oops, I missed the new. But to be fair both of my comments were, really, based on the code give in the first post (apart from struct BstNode which I initially just used a made up struct -- before I saw the additional code)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 08-24-2015, 07:43 AM
  2. pointer and smart pointer address
    By l2u in forum C++ Programming
    Replies: 14
    Last Post: 12-26-2006, 05:00 PM
  3. Should i pass address of pointer or just pointer???
    By howhy in forum C++ Programming
    Replies: 11
    Last Post: 09-02-2005, 04:05 AM
  4. Pointer to address.
    By Hulag in forum C Programming
    Replies: 6
    Last Post: 12-16-2004, 06:11 AM
  5. Pointer address
    By cheeves in forum C Programming
    Replies: 7
    Last Post: 10-30-2003, 08:26 PM