开发者

Is void *p = 0L valid?

In this answer, sassman initializes a pointer with:

zend_class_entry* ce = 0L;

My question is – is this valid? I would say it isn't, to initialize the variable with a null pointer either an unadorned (and possibly casted to void *) 0 constant, or some macro that evaluates to that such as NULL should be used.

However, I can't find definitive language in the standard that support开发者_运维问答s this interpretation. All it says is:

An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant.


0L is an integral constant expression with the value of zero. When used as a pointer, such a constant expression is a null pointer constant. That's exactly what the sentence you cite says (C99 6.3.2.3/3):

An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant.

This is followed immediately by a statement that a null pointer constant can be converted to a null pointer:

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.

Why do you think that 0 is not usable as a null pointer constant?


Absolutely any integral constant expression with zero value will be treated by the compiler as a null-pointer constant. 0L is a perfectly valid null-pointer constant, so the code is absolutely valid.


You can set all builtin POD types to the literal constant 0 without casting.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜