Why do I need to use “pointer to pointer” in this situation?

why I have to use "pointer to pointer" in the function "InsertAtFront"
This is the reason:

(*head) = newNode;

You set the head pointer to newNode. If head were not pointer to pointer you couldn't modify the pointer so that it applies outside of the function.
> why I have to use "pointer to pointer"

You don't have to use "pointer to pointer", a reference to pointer would also be ok.

1
2
3
4
5
6
void InsertAtFront( Node*& head, int newValue ) {
    // ...
    newNode->Next = head ;
    head = newNode;
    // ...
}
Note that head is passed by value. You can also pass head as Node*& ie a ref to Node*.

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
#include <iostream>
using std::cout;

class Node {
public:
	int Value {};
	Node* Next {};

	Node() {}
	Node(int v) : Value(v) {}
};

void InsertAtFront(Node*& head, int newValue) {
	const auto newNode {new Node(newValue)};

	newNode->Next = head;
	head = newNode;
}

int main() {
	auto head {new Node(1)};
	const auto second {new Node(2)};
	const auto third {new Node(3)};

	head->Next = second;
	second->Next = third;

	InsertAtFront(head, 4);

	for (auto itm {head}; itm; itm = itm->Next)
		std::cout << itm->Value << "  ";

	std::cout << '\n';

	while (head) {
		const auto nxt {head->Next};

		delete head;
		head = nxt;
	}
}



4  1  2  3

OP is a spammer.
seeplus wrote:
Note that head is passed by value.

This. Function parameters in C++ have two options: by value or by reference. C had only by value.

The confusion is due to the parameter being a pointer.

Note that modern C++ has alternatives to the use of raw pointers, so one can do much without pointers.

Lets take simple example:
1
2
3
4
5
6
7
8
9
int func( int two ) {
  two = 42; // does not change one
  return two;
}

int main() {
  int one = 7;
  std::cout << func( one );
}

The 'func' takes a parameter by value. The initial value of 'two' is copied from 'one' on the function call.
Changing 'two' within function does not change the caller's variable.

Lets use alias for int:
1
2
3
4
5
6
7
8
9
10
11
using T = int;

T func( T two ) {
  two = 42; // does not change one
  return two;
}

int main() {
  int one = 7;
  std::cout << func( one );
}

Nothing did change.

Now, lets make a function that does take by reference:
1
2
3
4
5
6
7
8
9
10
11
using T = int;

T reffunc( T& two ) {
  two = 42; // does change one
  return two;
}

int main() {
  int one = 7;
  std::cout << reffunc( one );
}

The 'two' is now a reference to 'one' and therefore the 'one' is changed by 'refrunc'.

Plan B. Pointer.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
using T = int;

T ptrfunc( T* two ) {
  *two = 5; // does change one
  // two points to one
  T three {};
  two = &three; // two now points to three. one did not change.
  *two = 42; // does change three. one did not change
  return *two;
}

int main() {
  T one = 7;
  std::cout << ptrfunc( &one );
}

Here 'two' is a pointer. Its value is an address. The value is initialized during function call. The value is a copy.
We can modify the caller's variable that the pointer points to.

However, when we change the value of the pointer, the 'two' no longer points to 'one'. It now points to 'three'.

Finally.
1
2
3
4
5
6
7
8
9
10
11
12
using T = int*;

void ptrfunc( T* two ) {
  *two = new T;
}

int main() {
  int one = 7;
  T head = &one; // head points to one
  ptrfunc( &head );
  // head does not point to one
}

On this call the 'two' points to 'head'.
The *two = new T; modifies the pointed to object just like *two = 5; did.
The only difference is that now pointed to object is a pointer (and hence 'two' is a pointer to pointer).
OP is a spammer.


Yeah. The original post has now been deleted - which sort of makes the replies somewhat nonsense. The original code was C++ and not c.
Topic archived. No new replies allowed.