# C++:指针的引用

记录一下刷题时,找了很久的一个bug,函数中对输入指针进行操作TreeNode* &node

#include<bits/stdc++.h>

using namespace std;

struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
    TreeNode(int x) :
            val(x), left(NULL), right(NULL) {
    }
};


void createTree(TreeNode* &node){
    node=new TreeNode(0);
    cout<<" node in function: "<<node<<endl;
    node->left=new TreeNode(12345);
    node->right=new TreeNode(54321);

}
int main(){

    TreeNode* root=NULL;
    cout<<" root before : "<<root<<endl;
    createTree(root);
    cout<<" root after : "<<root<<endl;

    return 0;
}

/* output
root before : 0
 node in function: 0xfb17a0
 root after : 0xfb17a0
 */
posted @ 2022-07-11 16:41  陈橙橙  阅读(46)  评论(0编辑  收藏  举报