+ 1

When to use **ptr, practical example?

Can anyone describe when exactly is the pointer to a pointer variable is used with a practical example plz? It would be much appreciated. Tnx in advance!

9th Nov 2018, 10:51 PM
Tamim Asefi
Tamim Asefi - avatar
2 Answers
+ 3
A list is just a pointer right? Like, a string is a list of characters, or char*. So your shopping list, a list of strings, is char**. If you are keeping all shopping lists you ever wrote in a folder, that's a char***. Also. You use pointers so functions can change the variables you pass in, right? void change_value(int* p){ *p += 2; } int x = 0; change_value(&x); // x is now 2. Similarly you can use a pointer to a pointer to change a pointer of your choice! void change_ptr(char** p){ *p += 2; } char* p = "hello world!"; change_ptr(&p); // p now points to "llo world!" Pointer pointers may seem hard and useless if you're not used to them, but I guarantee that one day you'll be working on a problem and pointer pointers will be the solution, and it will be obvious and you'll exactly know what to do :) At the end of the day a pointer pointer is like any other pointer.
10th Nov 2018, 12:38 AM
Schindlabua
Schindlabua - avatar
+ 1
Great explanation. You're awesome! Thanks a lot.
10th Nov 2018, 7:54 AM
Tamim Asefi
Tamim Asefi - avatar