NULL是指针吗? [英] Is NULL a pointer?
问题描述
因此,我与教授较早地争论过,认为NULL不是指针,但他一直坚持认为这是因为存在诸如NULL指针之类的东西.所以,现在我有点困惑NULL是否真的是一个指针
So, I had an argument with my professor earlier defending that NULL is not a pointer, but he kept on insisting that it is because there is such a thing as NULL pointer. So, here I am now a little bit confused if NULL is really a pointer or not
我已经尝试通过互联网搜索,但是找不到任何答案,所以我的最后选择是在这里
I already tried search over the internet but couldn't find any answer, so my last resort is here
推荐答案
在C中,NULL
是一个扩展为空指针常量的宏.
In C, NULL
is a macro that expands to a null pointer constant.
宏是
NULL,它扩展为实现定义的空指针常量; ...
NULL which expands to an implementation-defined null pointer constant; ...
空指针常量是具有值0
(
例如0
,1-1
,42*0LL
等)或强制转换为(void*)
的表达式.
A null pointer constant is an integer constant expression with the value 0
(
e.g., 0
, 1-1
, 42*0LL
, etc.) or such an expression cast to (void*)
.
值为0的整数常量表达式或此类表达式 强制转换为void *类型,称为空指针常量.66)如果为空 将指针常量转换为指针类型,结果 指针,称为空指针,保证将不等于与 指向任何对象或函数的指针.
An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant.66) If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed to compare unequal to a pointer to any object or function.
大多数常见的C实现将NULL
定义为0
,0L
或((void*)0)
.
Most common C implementations define NULL
to be 0
, 0L
, or ((void*)0)
.
所以您是是正确的. NULL
不必是指针.
So you are correct. NULL
need not be a pointer.
(IIRC,C ++甚至不允许将(void*)
强制转换为NULL
,这意味着C ++中的NULL
始终具有整数类型.因此,并且由于void*
指针
不要与C ++中的常规指针进行比较,因此C ++> = 11现在有一个特殊的nullptr
关键字.)
(IIRC, C++ doesn't even allow the (void*)
cast in NULL
, meaning NULL
in C++ always has integer type. Because of that and because void*
pointers
do not compare with regular pointers so readily in C++, C++>=11 now has a special nullptr
keyword.)
这篇关于NULL是指针吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!