为什么在 C 和 C++ 中对 NULL 指针的定义不同?
在 C++ 中,空指针可以通过空指针常量定义,空指针常量是值 0 的整型常量表达式,如 −
int*p = 0;
但在 C 语言中,空指针可以通过空指针常量定义,空指针常量是值 0 的整型常量表达式,或将这样的表达式强制转换为 void*,如 −
Int *p = 0;;
或
int*p = (void*) 0;
在 C++11 中,使用关键字“nullptr”表示空指针。
int* ptr = nullptr;
在 C 语言中
示例
#include <stdio.h>
int main() {
int *p= NULL; //initialize the pointer as null.
printf("The value of pointer is %u",p);
return 0;
}输出
The value of pointer is 0.
在 C++ 中
示例
#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;
}输出
The value of pointer is 0.
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP