/**************************************************************/ // R. A. Hillyard // pointer02.cpp // November 2001 // // Program to demonstrate the use of the new operator /**************************************************************/ #include<iostream> #include<iomanip> using namespace std; int main() { int *intPtr1, *intPtr2; //declare pointer variables intPtr1 = new int; //allocate memory for int and point to it intPtr2 = new int;
//print out value of uninitialized memory locations cout << "intPtrs value : " << *intPtr1 << " " << *intPtr2 << endl; cout << "intPtrs address: " << intPtr1 << " " << intPtr2 << endl;
//set the value using the defererencing operator *intPtr1 = 33; *intPtr2 = 22; cout << "intPtr1: " << intPtr1 << " " << *intPtr1 << endl; cout << "intPtr2: " << intPtr2 << " " << *intPtr2 << endl; *intPtr1 = *intPtr2 * 2; cout << "intPtr1: " << intPtr1 << " " << *intPtr1 << endl; cout << "intPtr2: " << intPtr2 << " " << *intPtr2 << endl; intPtr1 = intPtr2; cout << "intPtr1: " << intPtr1 << " " << *intPtr1 << endl; cout << "intPtr2: " << intPtr2 << " " << *intPtr2 << endl; } /***********************Program Output*************************/ intPtrs value : 1258860 1258876 intPtrs address: 0x00133560 0x00133570 intPtr1: 0x00133560 33 intPtr2: 0x00133570 22 intPtr1: 0x00133560 44 intPtr2: 0x00133570 22 intPtr1: 0x00133570 22 intPtr2: 0x00133570 22