In this pointers cpp program,
we have assigned as value of
mypointer a
reference to
firstvalue using the reference
operator (&). And then we have assigned the
value 5 to the memory location pointed by
mypointer, that
because at this moment is pointing to the
memory location of
firstvalue, this in fact modifies the
value of firstvalue.
#include <iostream.h>
using namespace std;
int main ()
{
int firstvalue, secondvalue;
int * mypointer;
//
www.freecprograms.com pointers in cpp
mypointer = &firstvalue;
*mypointer = 5;
mypointer = &secondvalue;
*mypointer = 10;
cout << "firstvalue is " << firstvalue <<
endl;
cout << "secondvalue is " << secondvalue <<
endl;
return 0;
}
Output
firstvalue is 5
secondvalue is 10
|