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

CC++Server Side ProgrammingProgramming

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.
raja
Published on 18-Jun-2019 12:30:47
Advertisements