C++ Pointer Create int type pointer and assign int variable address to it

Description

C++ Pointer Create int type pointer and assign int variable address to it

#include <iostream>
using namespace std;
int main()//  w  ww .j a va2s. c o  m
{
   int *numAddr;      // declare a pointer to an int
   int miles, dist;   // declare two integer variables
   dist = 158;        // store the number 158 in dist
   miles = 22;        // store the number 22 in miles
   numAddr = &miles;  // store the address of miles in numAddr
   cout << "The address stored in numAddr is " << numAddr << endl;
   cout << "The value pointed to by numAddr is " << *numAddr << "\n\n";
   numAddr = &dist;   // now store the address of dist in numAddr
   cout << "The address now stored in numAddr is " << numAddr << endl;
   cout << "The value now pointed to by numAddr is " << *numAddr << endl;
   return 0;
}



PreviousNext

Related