-1
#include <iostream>               

using namespace std;             

int main() {
   char* x=0;     //  initialize pointer character called x equals zero 
   cout<<x;       //  print the value of pointer in memory      
   cout<<5;       //  print 5 on screen  (not executed)  
}  

line 9 does not executed by the compiler

Jive Dadson
  • 16,680
  • 9
  • 52
  • 65
Mohamed Helmy
  • 89
  • 1
  • 3

2 Answers2

4

std::ostream's operator<< has an overload for char*, this dereferences the pointer you pass to it.

This means you try to dereference a pointer pointing to 0 resulting in undefined behavior. In this case, your next print is not shown.

If you want to show the address you should cast to void* explicitly:

cout<< static_cast<const void*>(x);
Hatted Rooster
  • 35,759
  • 6
  • 62
  • 122
0

You have assigned a char pointer variable x = 0 which is null and trying to dereference a null pointer which resulted in a undefined behaviour. Pointer variables should store only address of another variable

Eddy
  • 67
  • 7