#include <iostream>
using namespace std;
struct node{
int val;
node* left, *right;
};
void _delete(node *root)
{
root = NULL;
}
void change(node *root)
{
root->val = 6;
}
int main()
{
node * root = new node;
root->val = 5;
change(root);
_delete(root);
cout<<root->val<<endl;
return 0;
}
The output of the above program is 6. It seems as if the _delete
function has no effect on the root node but change
function has an effect on the root node. It is almost as if delete
treats the argument passed as a local variable but change
treats the argument as a global variable. Is there anything that I am missing out or is this normal? If it is normal, please explain.