« Double pointers
Understand double pointers
Simplify Double Pointers
1int i = 10;2int *p = &i;3int **dp = &p;45//same values for below 2 lines6cout << "&p: " << &p << endl;7cout << "dp: " << dp << endl;8910//same values for below 2 lines11cout << "p: " << p << endl;12cout << "*dp: " << *dp << endl;131415//same values for below 3 lines16cout << "i: " << i << endl;17cout << "*p: " << *p << endl;18cout << "**dp: " << **dp << endl;
Above means p
is a pointer to a int
and dp
is a pointer to int*
which is a pointer. So dp
is a double pointer
Passing double pointers to functions
1void increment1(int **p)2{3 p = p + 1;4}
1void increment2(int **p)2{3 *p = *p + 1;4}
1void increment3(int **p)2{3 **p = **p + 1;4}
Final code:
1#include <iostream>2#include <iomanip>3#include <algorithm>4#include <string>5#include <cstring>6#include <vector>7#include <cmath>8#include <map>9#include <climits>10// climits for INT_MIN11#include <unordered_map>12using namespace std;1314void increment1(int **p)15{16 p = p + 1;17}1819void increment2(int **p)20{21 *p = *p + 1;22}2324void increment3(int **p)25{26 **p = **p + 1;27}2829int main()30{31 int i = 10;32 int *p = &i;33 int **dp = &p;3435 cout << "&dp: " << &dp << endl;3637 // same values for below 2 lines38 cout << "&p: " << &p << endl;39 cout << "dp: " << dp << endl;4041 // same values for below 2 lines42 cout << "p: " << p << endl;43 cout << "*dp: " << *dp << endl;4445 // same values for below 3 lines46 cout << "i: " << i << endl;47 cout << "*p: " << *p << endl;48 cout << "**dp: " << **dp << endl;4950 return 0;51}