c++ - What is the difference between passing a pointer to function and passing reference of pointer to function? -


so, have structure

struct node {     node * l;     node * r; }; 

then, there is

typedef node* tnode; 

what don't understand function:

void tsplit(tnode t, tnode &l, tnode &r, int x) 

as it, pass t tsplit() pointer structure, pass 2 references of pointers structures of same type. why can't pass pointers instead of references? makes sense?

yes makes sense. can treat references pointers (with restrictions). change references pointers in example. of course syntax change in the, don't need bother it. example like:

void tsplit(tnode t, tnode *l, tnode *r, int x)

so difference is, can modify under l , r not t, typedef abstracts it, can expand it:

void tsplit(node* t, node** l, node** r, int x)

now, meaning can change under t, cannot change t itself, can l , r. in other words, cannot change reference target of t, can r , l, because have reference reference (or pointer pointer).

why use references on pointers? because pointers can used different things, changing ownership of objects. syntax same, semantics different. people @ how things look, , know intention , meaning behind it. i.e. deduct semantics syntax. references can used pass variables, know expect of things.


Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -