Why are NULL pointers defined differently in C and C++?


In C++, a null pointer can be defined by as a null pointer constant is an integer constant expression with the value 0, like −

int*p = 0;

But in c, a null pointer can be defined by as a null pointer constant is an integer constant expression with the value 0 or such an expression cast to void*, like −

Int *p = 0;;

Or

int*p = (void*) 0;

In C++11 a keyword “nullptr” is used to represent nullpointer.

int* ptr = nullptr;

In C

Example

 Live Demo

#include <stdio.h>
int main() {
   int *p= NULL; //initialize the pointer as null.
   printf("The value of pointer is %u",p);
   return 0;
}

Output

The value of pointer is 0.

In C++

Example

 Live Demo

#include <iostream>
using namespace std;
int main() {
   int *p= NULL; //initialize the pointer as null.
   cout<<"The value of pointer is ";
   cout<<p;
   return 0;
}

Output

The value of pointer is 0.

Updated on: 30-Jul-2019

111 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements