Here I am just giving few simple examples with added information in the program as comments or print statements.
Example 1: How to declare variable of pointer type.
// pointer example
#include <iostream>
using namespace std;
int main ()
{
float value1, value2;
float * pointr;
pointr = &value1;
cout << "'pointr' is address of 'value1'\n";
*pointr = 1.0; // assigning value = 1 to memory location pointed by pointr
cout << "above is the value pointed by '*pointr' and is equal to 'value1'\n";
cout << "value1 = " << value1 << endl;
pointr = &value2;
cout << "\n'pointr' is address of 'value2'\n";
*pointr = 2.0; // assigning value = 1 to memory location pointed by pointr
cout << "above is the value pointed by '*pointr' and is equal to 'value2'\n";
cout << "value2 = " << value2 << endl;
cout << "NOTE: Clearly a pointer 'pointr' can take many values in the same program \n";
return 0;
}
Output:
'pointr' is address of 'value1'
above is the value pointed by '*pointr' and is equal to 'value1'
value1 = 1
'pointr' is address of 'value2'
above is the value pointed by '*pointr' and is equal to 'value2'
value2 = 2
NOTE: Clearly a pointer 'pointr' can take many values in the same program
MORE to be added.