1

Program 1:

#include <iostream>
using namespace std;
int main ()
{
   int  *ptr = NULL;
   cout << "The value of ptr is " << ptr ;
   return 0;
}

Program 2:

#include <iostream>
using namespace std;
int main ()
{
   char  *ptr = NULL;
   cout << "The value of ptr is " << ptr ;
   return 0;
}

The question is,In the Program 1 the output is stable as :

  The value of ptr is 00000000

while,in the Program 2,instead of int pointer I used char pointer and it gives me an Exception.

can anyone help me with the understanding?

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Mukesh Pareek
  • 33
  • 1
  • 7

1 Answers1

3

Because the << operator isn't dereferencing the int* in the first program, it's simply printing the value of the pointer as 0. But in the second program the << operator is attempting to dereference the pointer as a string, which crashes.

kcraigie
  • 1,252
  • 6
  • 13