what does double * as parameter of a function means?

thats it... im doing a binary tree as homework, but my code was not working till' i added the double **... so i wonder what does it means...

this is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class BT{
    public:
        BT *tree,*left,*right;
        Person data;
        BT();
        void Insert(BT*, Person element);
};
BT::BT(){
    left=NULL;
    right=NULL;
}

void Tree::Insert(BT **tree, Person element){
    if(*tree==NULL){
        *tree = new BT;
        (*tree)->data = element;
    }
    else if(element.name<(*tree)->data.name){
        Insert(&((*tree)->left),elemento);
    }
    else{
        Insert(&((*tree)->right),element);
    }
}

int main(){
    Person pers;
    BT tre;
        pers.Capture();
        tre.Insert(&tre.tree,pers);
}


and why this is not working, the next code is like i usually implement a doubly linked list..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class BT{
    public:
        BT *tree,*left,*right;
        Person data;
        BT();
        void Insert(BT*, Person element);
};
BT::BT(){
    left=NULL;
    right=NULL;
}
void Tree::Insert(BT *tree, Person element){
    if(tree==NULL){
        tree = new BT;
        tree->data = element;
    }
    else if(element.name<tree->data.name){
        Insert(tree->left,elemento);
    }
    else{
        Insert(tree->right,element);
    }
}

int main(){
    Person pers;
    BT tre;
        pers.Capture();
        tre.Insert(tre.tree,pers);
}


thanks in advance.
so, it means a pointer that points a pointer?
Read from right to left: double * * is a pointer to a pointer to a double. Some people try to shorten "pointer to pointer" as "double pointer" but that creates confusion with "pointer to double".
Last edited on
so, it means a pointer that points a pointer?


Yes.

You have to remember that a pointer is still just a variable, it is passed by value to a function. This means that when tree is NULL and you call new on it, you are changing the value of the local "tree". This is not to be confused with changing the value of the dereferenced pointer.

Which is exactly why your solution works. You pass a pointer to the pointer, and in that way you can dereference it and change the value of the original tree.

You could also pass the pointer by reference, which will make things a little simpler:
1
2
3
4
5
6
7
8
9
10
11
12
13
void newDouble( double *& ptr )
{
  ptr = new double;
}

int main( void )
{
  double *ptr = nullptr;
  newDouble( ptr );
  ptr == nullptr; // false

  delete ptr;
}
thanks guys =)
"so, it means a pointer that points a pointer?"

Just in case you'd think it would end with pointer to pointer

1
2
3
4
5
6
7
8
9
int main ()
{
    double val = 10.0;
    double* pval = &val;
    double** ppval = &pval;
    double*** pppval = &ppval;
    double**** ppppval = &pppval;
    double***** ppppppval = &ppppval;
}

woah thats crazy, i have enough atm with pointer to pointer =P
Topic archived. No new replies allowed.