Null object: Difference between revisions

Content added Content deleted
(→‎{{header|C}}: Move a sentence from Undefined values#C that fits better here.)
m (→‎{{header|C}}: clarify "pointer zero")
Line 96: Line 96:
Classic BASIC does have the concept of un-initialised or undefined variables.
Classic BASIC does have the concept of un-initialised or undefined variables.
=={{header|C}}==
=={{header|C}}==
C has the null pointer. By convention, it points to memory address zero. It is (supposedly) garanteed to be pointing to nothing, so receiving one of those likely means you are not looking at an object--<i>but</i>, there are occasions where changing content of a null pointer actually does something (say, on DOS); and a function that's supposed to return a pointer on success doesn't always return a 0 otherwise (e.g. mmap returns -1 for failure).
C has the null pointer, written as "0", whose internal representation is often, [http://c-faq.com/null/varieties.html though not always], the same as integer zero. It is (supposedly) garanteed to be pointing to nothing, so receiving one of those likely means you are not looking at an object--<i>but</i>, there are occasions where changing content of a null pointer actually does something (say, on DOS); and a function that's supposed to return a pointer on success doesn't always return a 0 otherwise (e.g. mmap returns -1 for failure).


There is a very common macro, <code>NULL</code>, which evaluates to <code>(void*) 0</code> or an equivalent value. NULL is compatible with all pointer types, including both data pointers and function pointers.
There is a very common macro, <code>NULL</code>, which evaluates to <code>(void*) 0</code> or an equivalent value. NULL is compatible with all pointer types, including both data pointers and function pointers.