pointer to pointer?

hey guys,I understand what a pointer to pointer is a pointer to a pointer is a pointer that stores another pointer which in turn stores a variable but what has me confused lets take the example below

current is a pointer which points to a pointer head which points to a node;

now how come I don't declare current as **current and when I do I get an error but when I declare current as a single pointer it works *current any ideas?

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
  #include <iostream>

using namespace std;

struct node{

   int value;
   node *nextNode;


};

node *head = NULL;

node* getNewNode(){

    node *newNode = new node;
    newNode->value = 0;
    newNode->nextNode = head;
    head = newNode;
    return newNode;

}


int main()
{

   node *one = getNewNode();
   node *two = getNewNode();
   node *three = getNewNode();
   one->value = 10;
   two->value= 20;
   three->value = 30;

   node *current = head;

   while(current != NULL){

       cout << current->value << endl;
       current = current->nextNode;


   }

}
current is a pointer which points to a pointer head which points to a node;
No. current is a pointer to a node. Look what you're assigning to it. current = head, not current = &head.

1
2
3
4
5
6
7
node *current = head;
node **current2 = &current;

while (*current2 != NULL){
    cout << (*current2)->value << endl;
    *current2 = (*current2)->nextNode;
}
thanks for the reply,that makes a lot of sense now =)
Topic archived. No new replies allowed.