I understand that (char *) 0
is a char pointer which points an address at 0. So there is no memory address, out of bound. Often, I hav seen people using it instead of NULL
, so why don't they just simply use NULL
? Or, I would like to know, whether there is any special approach to it.

- 1,587
- 12
- 35
-
1`NULL` is a way of mentioning "Please don't point anywhere" to the pointer. If you don't make it `NULL` It will be pointing to some or the other address – sameera sy Mar 25 '16 at 15:55
-
1`NULL` is a macro. It might be defined as and would expand to `(void*) 0`. – alk Mar 25 '16 at 15:59
-
@alk do you mean any kind of `(void*) 0` style could be an underlying implementation of `NULL` ? – FZE Mar 25 '16 at 16:00
-
What do you mean by "*any kind of `(void*) 0` style*"? – alk Mar 25 '16 at 16:02
-
3The relevant part of the C11 spec is 6.3.2.3p3: *An integer constant expression with the value 0, or such an expression cast to type `void*`, is called a null pointer constant. 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.* and footnote 66: *The macro `NULL` is defined in `
` (and other headers) as a null pointer constant.* – Theodoros Chatzigiannakis Mar 25 '16 at 16:02 -
sorry about my bad english, I mean, (void *) 0 or (char *) 0 instructions different but the effect same. – FZE Mar 25 '16 at 16:03
-
Some compilers will complain if you use 0 without typecasting it, but will accept `NULL`. The resulting code is the same after compilation. – coladict Mar 25 '16 at 16:09
-
1"I understand that (char *) 0 is a char pointer which points an address at 0" - No, it is not! The C standard does not cover using absolute addresses. – too honest for this site Mar 25 '16 at 16:10
-
`(char*)0` has a binary representation of `0`, so (as per the Standard citied here http://stackoverflow.com/questions/36223261/is-there-any-special-approach-to-using-char-0-in-c#comment60078072_36223261) it is a null-pointer constant so its use is equivalent to the use of `NULL`. – alk Mar 25 '16 at 16:11
-
@coladict: A C compiler must not complain! Using the macro `NULL` which should contain an explicit cast to `void *` ist recommended, but not mandatory. – too honest for this site Mar 25 '16 at 16:11
-
@alk: Not really. It is not a null pointer constant. And the binary representation of a null pointer need not have all bits zero. – too honest for this site Mar 25 '16 at 16:13
-
Related: http://stackoverflow.com/q/2581522/694576 – alk Mar 25 '16 at 16:18
-
@Olaf: "*the binary representation of a null pointer need not have all bits zero.*" I did not say that. However `(char*) 0`'s binary representation *is* `0`. – alk Mar 25 '16 at 16:18
-
I tend to interpret the spec as saying that `(void*)0` is a null pointer constant and `(char*)(void*)0` is a null pointer. I can't speak about `(char*)0` because I've recently been bitten for assuming that there is an intermediate `void*` conversion where there wasn't one. Also related is 6.3.2.3p5, which declares integer conversions to pointer types to be implementation-defined. I'm unsure about whether `(char*)0` falls under that. – Theodoros Chatzigiannakis Mar 25 '16 at 16:22
-
@alk: Either it is a null pointer constant (which it is not), or not (i.e. a valid address in the environment). In the former, there is no requirement for all bits `0`, for the latter this is true. – too honest for this site Mar 25 '16 at 16:24
-
@TheodorosChatzigiannakis: Agreed. (And I fell into the same trap about the conversion). `(char *)0` is a _null pointer_ of type `char *`. As I see the standard, you in fact cannot convert the _integer-constant_ `0` to a pointer. Even something like `(char *)(uintptr_t)0` or `1-1` would not do the trick standard compliant. All would be easier with a dedicated keyword for a `nullptr`. – too honest for this site Mar 25 '16 at 16:28
-
But as absolute addresses are beyond the standard anyway, this is already implementation-specific - as is the representation of a _null pointer_. Problematic is dereferencing, but on most modern architectures physical address `0` is special anyway and for virtual addresses it is easy to reserve this address. – too honest for this site Mar 25 '16 at 16:31
-
@Olaf do you mean the absolute `0x0` could be a most common address for null pointer but it is not guaranteed ? – FZE Mar 25 '16 at 16:33
-
@Olaf Thanks for the clarification. I can mark a detailed answer as a best answer, if you write these as an answer. – FZE Mar 25 '16 at 16:45
4 Answers
A null pointer is strictly speaking no valid address encoding:
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.
Thus I try to avoid the phrase "address 0" when it comes to null pointers, because this is the encoding and a null pointer need not have all bits zero. An architecture could - for instance - use an unused bit in the pointer variables to tag it as invalid. For comparision e.g. bitops would be used instead of compare.
The most common encoding is "all bits zero", though so I will mostly concentrate on this. This encoding is used because most CPUs have special (and shorter/faster) instructions to store a bit value of 0x0
into a memory cell and compare with/test for 0x0
. Using a special bitpattern also makes comparing two pointers straight forward, using a single compare. The version with setting a dedicated bit would require additional instructions.
Also a value which is at the edge of the address range avoids gaps. This is true for "all bits 1", too, but with less CPU support and address-width dependency.
Note that most architectures very well do have a valid physical address 0
, so there is in fact a problem with this encoding for a null pointer without address translation. Luckily this is typically a special address and not used by normal application code. With virtual memory addressing, it is quite easy just to make address 0x0
invalid, so this is out of discussion.
On some systems, address 0x0
is used for code (PC-boot code/BIOS, embedded systems) or data-accesses (RAM/peripheral hardware registers). Strictly speaking you cannot convert the integer-constant 0
to a pointer without invoking the implicit conversion to a null pointer. So, if your system does not use the 0x0
encoding, there is a problem accessing address 0x0
with standard C syntax only. (FYI: this is one reason other languages use a special keyword for a null pointer constant, e.g pascal NIL
, Python None
, C++ nullptr
.)
The conversion of an integer to a pointer is implementation defined anyway, as is the null pointer encoding. Thus the conversion of a 0
to a pointer is well in-line with the common copy-only integer->pointer encoding without modification of the representation.
A problem arises when dereferencing such a pointer. Dereferencing a null pointer is undefined behaviour (UB). As both encodings are identical, there is no way for the CPU to detect if this is a valid access or a null pointer dereferencing. Even worse: the compiler is free to exploit this UB and generate arbitrary code (or none at all) if it detects something like char c = *((char *)0);
.
For a true variable (i.e. not const
qualified) pointer, this is normally not much of a problem, as that would require dynamic code analysis and in C no code for runtime-checks is generated by the compiler, but for the constant expression above, it could very well cause problems.

- 12,050
- 4
- 30
- 52
A null pointer is a special pointer value that is known not to point anywhere. What this means that no other valid pointer, to any other variable or array cell or anything else, will ever compare equal to a null pointer.
The most straightforward way to get a null pointer in your program is by using the predefined constant NULL, which is defined for you by several standard header files, including , , and . To initialize a pointer to a null pointer, you might use code like
#include <stdio.h>
int *ip = NULL;
and to test it for a null pointer before inspecting the value pointed to you might use code like
if(ip != NULL)
printf("%d\n", *ip);
It is also possible to refer to the null pointer by using a constant 0, and you will see some code that sets null pointers by simply doing
char *ip = 0;

- 2,400
- 3
- 25
- 35
-
`NULL` is not a constant, but a macro. C does not have symbolic constants other than _enum-constants_ which are always of type `int`. And the header to be used for this macro alone is `stddef.h`, unless another header is already used (but it does not hurt to include `stddef.h` in general). This because it is guaranteed for **all** implementations, including freestanding. – too honest for this site Mar 25 '16 at 16:41
Short answer: Whether you say
char *p1 = NULL;
or
char *p2 = (char *)0;
or
char *p3 = 0;
they are all pretty much equivalent. They're all legal, they'll all work, they'll all initialize the pointer variables to null pointer values. The differences between them have to do with style, not correctness.
Whoever wrote the code you saw may just have had a different notion about what constitutes good style.
(Now, if you're dealing with pointers of type other than char *
, using (char *)0
would be poor, and should at least generate warnings, and under suitably obscure circumstances might actually cause problems.)

- 45,437
- 7
- 70
- 103
NULL represent that the pointer isnt pointing to any address, NULL is a macro for (void *)0
so you can use (char *)0, it would still point to nothing and it'll be easier to explain the code

- 83
- 6
-
1if I saw someone using (char *) 0 in code it would be sent straight back to explain why – Tom Tanner Mar 25 '16 at 16:38
-
-
1NULL is clear enough. It's a guaranteed invalid pointer. It's part of the language, and should be as familair as printf, int and UINT_MAX to the reader. Therefore one asks why (char *)0 has been chosen. – Tom Tanner Mar 25 '16 at 16:55
-
@Mukesh It would be a bad idea to ask someone you work with what NULL means, because it's defined in the specification of the C language. – Theodoros Chatzigiannakis Mar 25 '16 at 16:56
-
yeah but for beginners i think it shd be clear, and no doubt dis is a bad practice, bt extra knowledge doesnt hurts – Mukesh Mar 25 '16 at 17:01
-
@Mukesh: There is no extra information given why the cast, but actually less. Because it is not clear if really a null pointer is meant by the cast, or the author really intended to have a pointer to address `0x0` - in ignorance about the implicit conversion. Showing the type is nonsense; with the same argument you'd have to cast for every assignment, which you hopefully agree is a really bad idea (I'd instantly fire someone showing up with such code). – too honest for this site Mar 25 '16 at 17:27