Thursday, 15 April 2010

c - Confused about accessing content of binary tree node with pointer -



c - Confused about accessing content of binary tree node with pointer -

if have struct looks this:

struct node { int key_value; struct node *left; struct node *right; };

and have search function looks this:

struct node *search(int key, struct node *leaf) { if( leaf != 0 ) { if(key==leaf->key_value) { homecoming leaf; } else if(key<leaf->key_value) { homecoming search(key, leaf->left); } else { homecoming search(key, leaf->right); } } else homecoming 0; }

why within search function, when comparing value leaf, instead of doing:

key < (*leaf)->key_value

is done key < leaf->key_value

isn't leaf pointer? first need dereference pointer , access value?

so, passing address function, , should first content pointed address, , value (key_value) right?

the pointer beingness dereferenced. leaf->key_value equivalent (*leaf).key_value. arrow operator implies dereferencing of pointer.

c pointers binary-tree

No comments:

Post a Comment