Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A null pointer is a special pointer value guaranteed not to point to any valid object or function in memory. It serves as a sentinel value at the language level, representing an uninitialized or intentionally empty memory address state. In C, a null pointer is created by assigning a null pointer constant to a pointer variable. The C standard defines a null pointer constant as any integer constant expression with the value 0, or such an expression cast to type void *. Valid source code representations include 0, 0L, 0x00, '\0', (1 - 1), or the standard macro NULL.
#include <stddef.h>

/* The NULL macro is provided by stddef.h and other standard headers.
   Internally, it is typically defined by the implementation as:
   #define NULL ((void *)0) 
   or simply:
   #define NULL 0 
*/

int *int_ptr = NULL;       /* Initialized using the standard macro */
char *char_ptr = 0;        /* Implicit conversion from the integer 0 */
double *dbl_ptr = (1 - 1); /* Evaluates to 0, valid null pointer constant */

The C23 nullptr Keyword

To resolve type ambiguity and safety issues associated with the traditional NULL macro (which may evaluate to an integer type rather than a pointer), the C23 standard introduced the nullptr keyword and the nullptr_t type. nullptr is a dedicated null pointer constant of type nullptr_t that implicitly converts to any pointer type but does not implicitly convert to integer types (except bool).
#include <stddef.h>

int *ptr = nullptr;     /* C23 standard: strictly typed null pointer */
nullptr_t np = nullptr; /* Base type of the nullptr constant */

Technical Characteristics

Memory Representation While a null pointer is represented in source code by an integer constant expression evaluating to zero (or nullptr), the underlying hardware bit-pattern is implementation-defined. The compiler translates the null pointer constant into the architecture’s specific null memory address. Although almost universally represented by all zero bits at the hardware level, the C standard does not strictly mandate this. Type Equivalence and Comparison A null pointer of one type is guaranteed to compare equal to a null pointer of any other type. When a null pointer constant is assigned to or compared with a pointer of a specific type, it is implicitly converted to a null pointer of that target type. A null pointer is also guaranteed to compare unequal to any pointer to a valid object or function.
int *ptr_a = NULL;
void *ptr_b = NULL;

/* Valid comparison: Evaluates to true without explicit casting */
if (ptr_a == ptr_b) {
    /* Both are null pointers */
}

/* Boolean evaluation: A null pointer evaluates to false (0) */
if (!ptr_a) {
    /* Executes because ptr_a is null */
}
Variadic Functions and NULL Passing the traditional NULL macro to a variadic function (such as printf or execl) without an explicit cast invokes Undefined Behavior. Because NULL can be defined simply as the integer 0, the compiler may push an integer onto the call stack instead of a pointer. If the variadic function expects a pointer, this type mismatch corrupts the stack or reads invalid data.
#include <stdio.h>

/* UNDEFINED BEHAVIOR: NULL may be passed as a 32-bit int, 
   but %p expects a pointer (potentially 64-bit) */
printf("Pointer: %p\n", NULL); 

/* CORRECT: Explicit cast ensures a pointer type is passed */
printf("Pointer: %p\n", (void *)NULL);

/* CORRECT (C23): nullptr inherently passes as a pointer type */
printf("Pointer: %p\n", nullptr);
Dereferencing and Undefined Behavior Because a null pointer does not resolve to a valid memory location, attempting to dereference it (accessing or modifying the value it points to) invokes Undefined Behavior (UB).
int *ptr = NULL;
int val = *ptr; /* UNDEFINED BEHAVIOR: Attempting to read from a null address */
*ptr = 10;      /* UNDEFINED BEHAVIOR: Attempting to write to a null address */
On modern operating systems with virtual memory management, dereferencing a null pointer typically results in a hardware exception, such as a Segmentation Fault (SIGSEGV). This occurs because the zero page of virtual memory is intentionally left unmapped by the OS to trap these exact memory access violations. Pointer Arithmetic Performing pointer arithmetic on a null pointer is strictly undefined behavior in C.
int *ptr = NULL;
ptr++; /* UNDEFINED BEHAVIOR: Arithmetic on a null pointer */
The C standard dictates that pointer arithmetic is only valid when the pointer points to an element of an array object (or one past the last element). Since a null pointer points to no object, any arithmetic operation violates this constraint.
Master C with Deep Grasping Methodology!Learn More