I just started reading POINTERS and made my first program in pointers but when I got output window It was really surprising for me to get an unexpected output for the first time,after that I ran example Ex4_06.cpp and when I edited some of It's parts then It also gave me some interesting results.
My program is given below and I just added my doubts in comments of doubted lines
Code:
#include<iostream>
using namespace std;
int main()
{
int *pnum{};
int num1{ 18 }, num2{ 117 };
cout << num2<<endl;// here num2 is giving the expected value as of initialised.
pnum = &num1;
num1 = *pnum + 18;
cout << "number 1 is " << num1 << endl
<< "it is at "
<< hex << pnum
<< " *pnum " << *pnum //here *pnum is giving output of 24 which has to be 18
<<endl<<num2;//as I didn't altered value of num2 by any mean so Supposed output has to be as per initialised value 117.
//but output which I'm getting is really strange it is 75.
pnum = 0;
pnum = &num2;
cout << endl << " *pnum " << *pnum;//here *pnum is giving 75 same as the value last num2 gave as in last cout.
num1 = *pnum * 10;//as in above line of code *pnum is giving 75 so supposed output is 75*10=750 but output I got is 492.
cout << endl << "Now number 1 is " << num1 << endl
<< "It is at " << pnum << " and at that address *pnum is " << *pnum << endl;
return 0;
}
OUTPUT which I'm getting is
117
number 1 is 36
it is at 0043FEB4 *pnum 24
75
*pnum 75
now number 1 is 492
It is at 0043FEA8 and at that address *pnum is 75
Press any other key to continue...
I don't know what is wrong Please help me to figure it out.