for example I have a struct now
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
I first have a pointer, but I do not allocate space for it, so just
TreeNode* p;
I want to allocate the space later, for example, in the other function, like below
void allo(TreeNode* p, int val){
p = new TreeNode(val);
}
allo(p,5);
// I want to get the original p here
But this new method change the address of the original p, which I could not trace it anymore. So is there a method to allocate without changing the address? The limitation is that I want to separate the definition and the allocation, just like above, for example, define outside a function and allocate inside the function.