NULL, nullptr and Address Zero Explained
NULL is a null pointer constant, nullptr is its typed C++ and C23 replacement, and address zero is just an address that most systems leave unmapped.

Three things get called "null" in C and C++ code, and they are not the same thing. NULL is a macro. nullptr is a keyword with its own type. Address zero is a number that a hardware address can hold. A crash on *p where p is null happens because of the third one, but the language rules that decide whether the program is even valid come from the first two. This post pulls them apart, using GCC 14 and Clang 19 on Linux for the examples, with C17 as the baseline and C23 and C++11 for nullptr.
A null pointer is a promise, not an address
The C standard defines a null pointer constant as an integer constant expression with the value 0, or such an expression cast to void *. Assigning one to a pointer produces a null pointer, which is guaranteed to compare unequal to a pointer to any object or function. That is the whole contract. It says nothing about where in memory the pointer points, because it points nowhere.
NULL is a macro defined in <stddef.h> (and in <stdio.h>, <stdlib.h>, <string.h> and a few others) that expands to an implementation-defined null pointer constant. On glibc with GCC in C mode it expands to ((void *)0). In C++ it expands to something else, covered below.
#include <stdio.h>
#include <stddef.h>
int main(void) {
int *p = NULL;
int *q = 0;
printf("p is %s\n", p ? "non-null" : "null");
printf("p == q: %d\n", p == q);
printf("p prints as: %p\n", (void *)p);
return 0;
}p is null
p == q: 1
p prints as: (nil)Both NULL and a plain 0 are null pointer constants, so both assignments are valid and both pointers compare equal. The (nil) text comes from glibc's printf, which prints a null pointer that way for %p. Other C libraries print 0x0 or 0, because the standard leaves the format of %p to the implementation.
Note that the constant 0 is special only as a literal. Writing int zero = 0; int *p = zero; is not valid C, because zero is a variable, not a constant expression. That distinction matters later when we get to why C++ needed a new keyword.
Null does not have to be all-bits-zero
Here is the part that surprises people who learned C on a desktop. The standard does not say a null pointer is stored as all-zero bits. It says converting the constant 0 to a pointer type yields the null pointer, and the compiler is free to pick any representation as long as that conversion and the comparisons behave. Historic machines used non-zero patterns. Every platform you are likely to target today, including x86-64, AArch64, RISC-V and Cortex-M, stores a null pointer as all zero bits.
The practical consequence is small but real. memset(&p, 0, sizeof p) is not guaranteed by the standard to produce a null pointer, and neither is calloc for a struct that contains pointers. On the platforms above it works, and nearly all code relies on it. If you write code that must be portable to an exotic target, assign NULL explicitly. For everything else, treat zero-initialization as safe and move on.
Dereferencing null is undefined, and Linux makes it crash
Reading or writing through a null pointer is undefined behavior in the language. The compiler may assume it never happens, which is why a later section shows how a null check can vanish. What you see on a typical Linux program is a segmentation fault, but that is a property of the operating system, not the language.
#include <stdio.h>
int main(void) {
int *p = NULL;
printf("about to read\n");
printf("%d\n", *p);
return 0;
}about to read
Segmentation fault (core dumped)The first line prints because stdout is line-buffered on a terminal and the newline flushes it. Then the load from address zero faults. The CPU asks the memory management unit to translate virtual address 0, the process has no page mapped there, and the kernel delivers SIGSEGV. (The "core dumped" suffix depends on the ulimit -c setting and the shell, and may be absent.)
The kernel also stops you from mapping that page yourself by accident. The sysctl vm.mmap_min_addr sets the lowest address an unprivileged process may map, and most distributions default it to 65536. A call like mmap((void *)0, 4096, ..., MAP_FIXED, ...) therefore fails with EPERM for an ordinary user. That floor exists so that a kernel bug that dereferences a null pointer cannot be turned into code execution by a user process that mapped page zero first.
A field access on a null struct pointer, such as p->count with p == NULL, reads address offsetof(struct s, count), not address zero. Small offsets still land in the unmapped first page and fault. A huge struct or an array index can reach past that floor and read mapped memory silently.
When address zero is a real address
Everything above assumes a hosted system with virtual memory. On a microcontroller there is often no such thing. A Cortex-M part maps its flash or its vector table at address 0x00000000, so *(uint32_t *)0 reads the initial stack pointer value without any fault. A null dereference there does not crash. It returns plausible data and the program carries on wrong, which is far harder to debug than a segmentation fault.
That is also why the C language keeps "null pointer" separate from "address zero". A compiler for such a target can still use all-zero bits for null and treat any access through it as undefined. On bare metal you write *(volatile uint32_t *)0x0 only when you mean the hardware at that address, and the code stays correct only because you wrote the cast explicitly.
A related hazard is code that stores a hardware address as an integer and tests it against 0. If address zero is valid on your target, "zero means missing" is a bug waiting for the day a device lands there. Use a separate flag, or a pointer type, so that null remains the one value that means no object.
Why C++ added nullptr
In C++ the macro NULL cannot be ((void *)0), because C++ does not convert void * to other pointer types implicitly. Implementations define it as an integer constant, usually 0, 0L, or in GCC the built-in __null. That makes NULL an integer, and integers take part in overload resolution as integers.
#include <cstdio>
void f(int) { std::puts("int"); }
void f(const char *) { std::puts("pointer"); }
int main() {
f(0);
f(nullptr);
return 0;
}int
pointerf(0) picks the integer overload because 0 is an int first and a null pointer constant only by conversion. f(nullptr) picks the pointer overload because nullptr has the type std::nullptr_t, which converts to any pointer type and to nothing else. Passing NULL to this pair is where the old trouble lives: depending on the compiler and the exact overloads, it either calls the integer version or is rejected as ambiguous, and GCC warns "passing NULL to non-pointer argument" in the first case. C++11 introduced nullptr to remove that whole class of mistake.
The type also helps templates. A function template that forwards its argument deduces int from 0 and then fails to convert that int to a pointer parameter, but it deduces std::nullptr_t from nullptr and the conversion works.
nullptr arrives in C23
C23 copied the idea. nullptr is now a keyword in C with the type nullptr_t, which is declared in <stddef.h>. GCC 13 and Clang 16 accept it, and GCC 14 and Clang 19 spell the mode -std=c23.
#include <stdio.h>
#include <stddef.h>
int main(void) {
int *p = nullptr;
nullptr_t n = nullptr;
printf("p == n: %d\n", p == n);
printf("sizeof(nullptr_t) == sizeof(void *): %d\n",
sizeof(nullptr_t) == sizeof(void *));
return 0;
}p == n: 1
sizeof(nullptr_t) == sizeof(void *): 1Build it with gcc -std=c23 nullptr-c23.c. The C23 text requires nullptr_t to have the same size and alignment as void *, which is why the second line prints 1. The == comparison works because nullptr_t values are null pointer constants of their own type.
In C, you do not need to rewrite old code. NULL still works, and C23 allows the macro to expand to nullptr. The gain is in variadic calls and generic selection, where a bare 0 or ((void *)0) could be passed with the wrong width or the wrong type.
Which spelling to use
Use nullptr in C++11 and later, always. Use NULL in C17 and earlier, and nullptr in C23 projects that already require the new standard. Do not write a bare 0 for a pointer in either language. The compiler accepts it, but a reader cannot tell whether the author meant a number or "no object".
The deeper habit is the one worth keeping. Treat null as a value that means "there is no object here", check it before use at the boundary where it can occur, and never rely on the operating system to catch the mistake for you, because on some targets it will not.


