Understanding Pointers in C ++

I am trying to understand how C ++ pointers work, and I have a few doubts that I hope someone here helps me.

Let's say I have a structure as follows:

struct node
{
    int val;
    node *n1;
    node **n2;
};

I also have a function as follows:

void insertVal(node *&head, node *&last, int num)

My questions:

  • What does it mean n2? What is the difference between using '*'and '**'?

  • In function, what does it mean *&? I noticed that in the linked list, the implementation for insertion (in the tutorial that I saw) was '*&'used instead of just '*'why it is?

My apologies if this question is stupid, but I'm struggling to figure it out. Thank.

EDIT: , , **. : http://www.sanfoundry.com/cpp-program-implement-b-tree/. - , ** , , .

+4
1
  • n2?

, , . , , , , node, :

node *n = new node;
n->val = ...;
n->n1 = ...;
n->n2 = new node*[5];
n->n2[0] = new node;
n->n2[1] = new node;
n->n2[2] = new node;
n->n2[3] = new node;
n->n2[4] = new node;

'*' '**'?

node node, :

node n;
node *pn = &n;
node **ppn = &pn;
  1. , *&?

(&) (*). , :

void insertVal(node* &head, node* &last, int num)

, ( , ) '*&' '*', ?

, , , , :

void insertVal(node* &head, node* &last, int num)
{
    ...
    // head and last are passed by reference, so any
    // changes made here are reflected in the caller...
    head = ...;
    last = ...;
    ...
}

node *head = ...;
node *last = ...;
...
insertVal(head, last, ...);
// head and last contain new values here ...

& ( *) , :

void insertVal(node* head, node* last, int num)
{
    ...
    // head and last are passed by value, so any changes
    // made here are not reflected in the caller...
    head = ...;
    last = ...;
    ...
}

node *head = ...;
node *last = ...;
...
insertVal(head, last, ...);
// head and last still have their original values here ...
+4

All Articles