Create int type pointer and assign int variable address to it - C++ Data Type

C++ examples for Data Type:Pointer

Description

Create int type pointer and assign int variable address to it

Demo Code

#include <iostream>
using namespace std;
int main()/*  w  w w .j  a v  a2 s  . 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;
}

Result


Related Tutorials