Yes, with a C++ we have a lot of options.
One of them is to have pointers pointing to pointers.
To simplify explanation, it's memory address where as real value we have another memory address.
Pointer to pointer is created using this symbol: "**
".
First initial variable, for example, integer:
int Number = 5000;
Then pointer to variable:
int *primPtr;
Then pointer to pointer:
int **secPtr;
First pointer targets original variable:
primPtr = &Number;
cout << "Address of actual Variable: " << primPtr << endl;
Second pointer targets first pointer:
secPtr = &primPtr;
cout << "Address of Address: " << secPtr << endl;
And now fun stuff - we can get real value using variable, first or second pointer:
cout << "Actual Value - direct from Variable: " << Number << endl;
cout << "Actual Value - using prim. pointer: " << *primPtr << endl;
cout << "Actual Value - using sec. pointer: " << **secPtr << endl;
Full script:
#include <iostream>
using namespace std;
int main() {
int Number = 5000;
int *primPtr;
int **secPtr;
primPtr = &Number;
cout << "Address of actual Variable: " << primPtr << endl;
secPtr = &primPtr;
cout << "Address of Address: " << secPtr << endl;
cout << "Actual Value - direct from Variable: " << Number << endl;
cout << "Actual Value - using prim. pointer: " << *primPtr << endl;
cout << "Actual Value - using sec. pointer: " << **secPtr << endl;
return 0;
}
Result:
Address of actual Variable: 0x6dfee8
Address of Address: 0x6dfee4
Actual Value - direct from Variable: 5000
Actual Value - using prim. pointer: 5000
Actual Value - using sec. pointer: 5000
Process returned 0 (0x0) execution time : 0.075 s
Press any key to continue.
No comments:
Post a Comment