+ 1

Pointer based confusion

https://code.sololearn.com/cKkCzKey7217 Why the append and push function have a pointer to pointer (head_ref), but the insertAfter function has a normal pointer (prev_node)? Can I do use a normal pointer instead of a pointer to pointer for the same?

16th Oct 2021, 2:42 PM
Kashyap Kumar
Kashyap Kumar - avatar
2 Antworten
+ 4
Why we have to pass a pointer to a pointer to the (mentioned) functions? Try that with just a normal pointer, it won't work(come on try it). It's because, when you pass a pointer to a function which has to modify head node, the pointer address is copied to the function argument. When you change the head node in the function, only the value of the function pointer changes. It happens because the pointer address is passed by value! So to overcome this, you pass a pointer to the head pointer, and more you're able to modify the real head node. So this is the reason why we pass pointer to pointer and not just a pointer. Yeah it's a little confusing. I'll try to provide any YouTube video link or some internet resource for better understanding. All the best =) https://practice.geeksforgeeks.org/problems/why-use-double-pointer-in-linkedlist
16th Oct 2021, 4:46 PM
Rishi
Rishi - avatar