- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
#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
#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.
Advertisements