Pointers to pointers in C ++

A pointer to a cursor is an unidirected form or a string of pointers. Typically, a pointer contains the address of a variable.

A pointer to a cursor is an unidirected form or a string of pointers. Typically, a pointer contains the address of a variable. When we define a pointer to a pointer, the first pointer contains the address of the second pointer, which points to the location that contains the actual value shown in the diagram below:

Pointers to pointers in C ++ Picture 1Pointers to pointers in C ++ Picture 1

A variable, which is a pointer to a pointer, must be declared. This is done by placing an asterisk (*) in front of its name. For example, the following is a pointer to a pointer to an int:

 int ** var ; 

When a target value is pointed by a pointer to a pointer, accessing that value requires that the asterisk operator be applied twice, as in the example:

 #include using namespace std ; int main () { int var ; int * ptr ; int ** pptr ; var = 125 ; // lay dia chi cua var ptr = & var ; // lay dia chi cua ptr boi su dung dia chi cua toan tu & pptr = & ptr ; // Lay gia tri boi su dung pptr cout << "Gia tri cua var la: " << var << endl ; cout << "Gia tri tai *ptr la: " << * ptr << endl ; cout << "Gia tri tai **pptr la: " << ** pptr << endl ; return 0 ; } 

Running the above C ++ program will produce the following results:

Pointers to pointers in C ++ Picture 2Pointers to pointers in C ++ Picture 2

According to Tutorialspoint

Previous article: Array of pointers in C ++

Next lesson: Pass the cursor to the function in C ++

5 ★ | 1 Vote