-1

So, I have a structure

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

Then, there is

typedef node* tnode;

What I don't understand is this function:

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

As I get it, we pass t to tsplit() as a pointer to a structure, than we pass two references of pointers to structures of the same type. Why can't we just pass pointers instead of their references? Does it makes any sense?

Niall
  • 30,036
  • 10
  • 99
  • 142
zardos
  • 5
  • 3
  • 2
    If you "can't understand" something, why waste time on getting an explanation? Explanations help people who *don't* understand something but are able to achieve understanding. – Kerrek SB Jun 25 '15 at 10:27
  • 1
    Passing a non const reference of tnode to tsplit(...) gives the function a chance to manipulate l and r (letting them point to another address) – Oncaphillis Jun 25 '15 at 10:29
  • 4
    @KerrekSB Probably because OP is not the best with english language, and wasn't aware of the subtle, yet logically very important difference. =P – luk32 Jun 25 '15 at 10:29
  • [Here](http://herbsutter.com/2013/06/05/gotw-91-solution-smart-pointer-parameters/) is an overview with recommendations on how to pass references, raw and smart pointers. – davidhigh Jun 25 '15 at 10:31
  • The references allow `tsplit` to manipulate the pointers themselves, not just the contents of what is being pointed to (each `tnode`). The function probably allocates memory or such. This is a typical "return by reference" issue, and the return values are pointers. – Niall Jun 25 '15 at 10:40

3 Answers3

0

The references allow tsplit() to manipulate the pointers themselves, not just the contents of what is being pointed to (each tnode). The function probably allocates memory or such.

This is a something of a typical/classical "return by reference" (also called out parameter) issue, and the return values here are pointers.

user703016
  • 37,307
  • 8
  • 87
  • 112
Niall
  • 30,036
  • 10
  • 99
  • 142
0

Yes it makes sense. Generally you can treat references as pointers (with some restrictions). So you could change references to pointers in your example. Of course the syntax would also change in the, but we don't need to bother with it. Your example would look like:

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

So the difference is, that you can modify what is under l and r but not t, the typedef abstracts it, but you can expand it:

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

Now, the meaning is that you can change what is under t, but you cannot change the t itself, you can do this with l and r. In other words, you cannot change the reference target of t, but you can do it with r and l, because you have a reference to a reference (or pointer to a pointer).

Why use references over pointers? Because pointers can be also used for different things, like changing ownership of the objects. The syntax would look the same, but the semantics are very different. People like to look at how things look, and immediately know what is the intention and the meaning behind it. I.e. deduct semantics from the syntax. References can be only used to pass variables, so you know what to expect just from the look of things.

luk32
  • 15,812
  • 38
  • 62
-1

yeah it makes sense , If you allocate new memory inside void tsplit(tnode t, tnode &l, tnode &r, int x)

like t = new struct node;

then it will be reflected back only if , you declared it as reference to pointer.

mystic_coder
  • 462
  • 2
  • 10