4 ms·
But NULL must, broadly speaking, be the same as zero. C standard 3.2.2.3: "An integral constant expression with the value 0, or such an expression cast to type
by pcwalton 12y ago
But NULL must, broadly speaking, be the same as zero.
C standard 3.2.2.3: "An integral constant expression with the value 0, or such an expression cast to type void * , is called a null pointer constant."
C standard 4.1.5: "The macros are... NULL, which expands to an implementation-defined null pointer constant."
See [1] for more info.
[1]: http://stackoverflow.com/questions/2599207/can-a-conforming-c-implementation-define-null-to-be-something-wacky http://stackoverflow.com/questions/2599207/can-a-conforming-...
- comex 12y agoThe constant 0 has to convert to NULL, but nonconstant zeroes don't, and NULL doesn't have to be 0 when converted back to an integer. This would break code like the example offsetof implementation. See also http://stackoverflow.com/questions/2597142/when-was-the-null-macro-not-0 http://stackoverflow.com/questions/2597142/when-was-the-null...
- notacoward 12y agoCasting zero to a pointer yields a null pointer. That doesn't preclude other-valued null pointers, or ensure that a cast the other way will yield zero. That's where the "undefined" part comes from.
- mpyne 12y agoThe way I like to think about it is that integer zero is the name in the C/C++ language (and runtime) for the actual null pointer, nothing more or less. In particular you can't assume anything about the integer representation of the actual null pointer.
- _kst_ 12y agoCasting a constant zero to a pointer type yields a null pointer. The conversion is done at compile time, so the compiler knows that a null pointer is the required result. There is no requirement for an integer-to-pointer conversion to retain the bit pattern of the integer value, any more than an integer-to-floating-point conversion is required to do so. In most modern implementations, conversions among integer and pointer types of the same size are trivial, simply reinterpreting the bits as a value of a different type. A conforming C or C++ compiler can use, for example, the bit pattern 0xFFFFFFFF to represent a null pointer. Converting a constant 0 to a pointer type would yield a pointer with the representation 0xFFFFFFFF. Converting a non-constant 0 to a pointer type does not necessarily yield a null pointer; the result is implementation-defined, and may be indeterminate garbage.
- EpicEng 12y agoNULL must compare equal to 0. That doesn't mean that it is represented by all 0 bits. NULL can have a value of anything add long as NULL == 0 is true.
- deleted 12y ago[deleted]
- deleted 12y ago[deleted]