How Pointer to Pointer Array in C++ works? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How Pointer to Pointer Array in C++ works?

I am finding it difficult to understand how pointer to pointer array works. I come across the following code in my book, but still unable to understand how it all works. https://code.sololearn.com/cxj6C4jHcxOf/?ref=app

30th Mar 2019, 6:40 PM
Abhimanyu Gupta
Abhimanyu Gupta - avatar
2 Answers
+ 2
You have to understand, that every variable can be defined as a certain value stored in a certain place in the memory. This applies to pointers too, their value is a address of another variable. When you use variable name in your code, you are accessing its value. When you use '&' symbol before it, you are accrssing its address. By using '*' before an address, you are accessing a value stored on the address. This can go into depth. You can have a pointer storing an address to an actual variable in some address in your computer memory, then yoi can have a dofferent pointer pointing to that address, meaning it storess the address of the first pointer. Here is an example: int var = 73; //Actual variable cout<<"var : value: "<<var<<" address: "<<&var<<endl; int *ptr1 = &var; //Pointer pointing to the variable cout<<"ptr1: value: "<<ptr1<<" address: "<<&ptr1<<endl; int **ptr2 = &ptr1; //Pointer pointing to the pointer cout<<"ptr2: value: "<<ptr2<<" address: "<<&ptr2<<endl;
30th Mar 2019, 8:15 PM
Jan Štěch
Jan Štěch - avatar
+ 2
You can see that address of var is the same as value of ptr1. And address of ptr1 is the same as value of ptr2. &ptr2 on the last line means address of ptr2, which must be written as &ptr2. https://code.sololearn.com/czWHF620aD67/?ref=app
30th Mar 2019, 8:20 PM
Jan Štěch
Jan Štěch - avatar