a value indicating that a pointer does not refer to a valid object
In computing, a null pointer (sometimes shortened to nullptr or null) or null reference is a value indicating that the pointer or reference does not refer to an object. Programs routinely use โ€ฆ Wikipedia
๐ŸŒ
Wikipedia
en.wikipedia.org โ€บ wiki โ€บ Null_pointer
Null pointer - Wikipedia
3 weeks ago - In computing, a null pointer (sometimes shortened to nullptr or null) or null reference is a value indicating that the pointer or reference does not refer to an object. Programs routinely use null pointers to represent conditions such as the end of a list of unknown length or the failure to ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ null-pointer-in-c
NULL Pointer in C - GeeksforGeeks
On some machines, the above would compile successfully but crash when the program is run. Though that doesn't mean it would show the same behavior across all the machines. As the value of NULL in predefined libraries is 0 and the pointer (that's pointing to NULL) is not pointing to any memory location, this behavior occurs.
Published ย  January 10, 2025
Discussions

Trying to understand NULL pointers
NULL is equivalent to 0 and equivalent to nullptr (in C++). It's an address that is "universally invalid." You can store 0x2093704802934 in a pointer and it will be invalid too (I'm sure) but it's not a convention. NULL is this convention that says "the pointer is not valid" and/or "the pointer hasn't been initialized yet" or "the memory pointed to has been released and I have invalidated the pointer by setting it to NULL." You store this value in a "pointer", like "int* s = 0" to show that it is not pointing to anything. And in your code you will check whether the pointer (s in my example) is equal to NULL (invalid) or not (valid). First you create a pointer, then you initialize it, then you use it, then you release it. More on reddit.com
๐ŸŒ r/learnprogramming
18
1
December 30, 2021
function - C Assign Pointer to NULL - Stack Overflow
I am misunderstanding something basic about pointers in C, this should be simple but search brings up nothing. I do not understand the behaviour of the following code; #include #i... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Why am I being asked to set pointer to Null after freeing?
Once the memory is freed it's no longer valid for use so you don't want anything pointing to it. You can also test a pointer for null before you use it. More on reddit.com
๐ŸŒ r/learnprogramming
32
12
October 19, 2022
Is it possible to initialize a C pointer to NULL? - Stack Overflow
I had been writing things like char *x=NULL; on the assumption that char *x=2; would create a char pointer to address 2. But, in The GNU C Programming Tutorial it says that int *my_int_ptr ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
People also ask

Can a null pointer be compared with other pointers?
Yes, null pointers can be compared to other pointers. This is a common practice to ensure that a pointer has been assigned a valid memory address. For example, checking whether a pointer is null before accessing it helps programmers avoid dereferencing errors. Such comparisons are straightforward and widely used in safe memory management.
๐ŸŒ
lenovo.com
lenovo.com โ€บ home
Null Pointer in Programming โ€“ Definition, Uses, and Common Pitfalls ...
What is the difference between a null pointer and an uninitialized pointer?
A null pointer explicitly points to โ€œnothingโ€ and holds a defined null value, like zero, while an uninitialized pointer contains garbage or unpredictable data. Unlike null pointers, uninitialized pointers pose a significant risk of leading the program to access invalid memory, potentially causing serious errors.
๐ŸŒ
lenovo.com
lenovo.com โ€บ home
Null Pointer in Programming โ€“ Definition, Uses, and Common Pitfalls ...
What is the purpose of a null pointer in programming?
The main purpose of a null pointer is to serve as a placeholder when a pointer does not yet point to a valid memory location or object. It signals the absence of a value, ensuring that the program doesnโ€™t attempt to access inaccessible or undefined memory. Additionally, null pointers are used to mark the termination of data structures like linked lists, simplifying logic and reducing errors.
๐ŸŒ
lenovo.com
lenovo.com โ€บ home
Null Pointer in Programming โ€“ Definition, Uses, and Common Pitfalls ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ what-is-a-pointer-to-a-null-pointer
What is a Pointer to a Null pointer - GeeksforGeeks
July 12, 2025 - But the variable of Null pointer takes some memory. Hence when a pointer to a null pointer is created, it points to an actual memory space, which in turn points to null. Hence Pointer to a null pointer is not only valid but important concept.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ trying to understand null pointers
r/learnprogramming on Reddit: Trying to understand NULL pointers
December 30, 2021 -

Hello all again,

I have another stupid question here lol, so I'm trying to wrap my head around NULL. Im currently under the impression that NULL is a built in constant that has a value of zero, but what does that actually mean? When would it be appropriate to use null? If someone could explain it in layman's terms that would be super helpful!

Top answer
1 of 6
48

It's because the pointer is passed by value and not by reference. If you want to change the pointer inside the function you need to pass the actual pointer as a pointer, i.e. a pointer to a pointer:

Copyvoid my_function(char **a)
{
    *a = NULL;
}

Use the address-of operator & when you call the function to get the address of the pointer:

Copymy_function(&ptr);
2 of 6
8

Your statement a=NULL in my_function() indeed sets the value of a to NULL, but a is a local variable of that function.When you passed ptr to my_function() in main(), the value of ptr was copied to a.I suppose your whole confusion arose from the * used before a in the definition of my_function().

Pointers are generally passed to functions when we want to manipulate the original values which those pointers point to, from the called function, and this is done by dereferencing those pointers from the called functions.In this case, had you used this:

Copy*a= blah blah;

it would have reflected in the value at the address pointed to by ptr in main().But since you want to change the value of ptr itself, you need to be able to have a way to manipulate it from my_function().For this you use a pointer-to-pointer,ie of type char**.You pass such a char** as argument to my_function(() and use it to alter the value of ptr.Here's the variation to your code that would do it for you:

Copy#include <stdlib.h>
#include <stdio.h>

void my_function(char **); // Change char* to char**

int main(int argc, char *argv[]) {
    char *ptr;
    ptr = malloc(10);

    if(ptr != NULL) printf("FIRST TEST: ptr is not null\n");
    else printf("FIRST TEST: ptr is null\n");

    my_function(&ptr); //You pass a char**

    if(ptr != NULL) printf("SECOND TEST: ptr is not null\n");
    else printf("SECOND TEST: ptr is null\n");
}

void my_function(char **a) {  //Change char* to char** here
    *a = NULL;
}
๐ŸŒ
Learn C++
learncpp.com โ€บ cpp-tutorial โ€บ null-pointers
12.8 โ€” Null pointers โ€“ Learn C++
August 12, 2015 - In the prior lesson, we also noted that pointers do not need to point to anything. In this lesson, weโ€™ll explore such pointers (and the various implications of pointing to nothing) further. ... Besides a memory address, there is one additional value that a pointer can hold: a null value.
Find elsewhere
๐ŸŒ
GNU
gnu.org โ€บ software โ€บ c-intro-and-ref โ€บ manual โ€บ html_node โ€บ Null-Pointers.html
Null Pointers (GNU C Language Manual)
A pointer value can be null, which means it does not point to any object. The cleanest way to get a null pointer is by writing NULL, a standard macro defined in stddef.h. You can also do it by casting 0 to the desired pointer type, as in (char *) 0.
๐ŸŒ
Unstop
unstop.com โ€บ home โ€บ blog โ€บ null pointer in c | a detailed explanation with examples
Null Pointer In C | A Detailed Explanation With Examples
May 3, 2024 - Any pointer which is assigned the value NULL becomes a null pointer. ... Pointers are powerful programming tools that store the memory address of variables, etc., elements of a program.
๐ŸŒ
Lenovo
lenovo.com โ€บ home
Null Pointer in Programming โ€“ Definition, Uses, and Common Pitfalls | Lenovo US
The main purpose of a null pointer is to serve as a placeholder when a pointer does not yet point to a valid memory location or object. It signals the absence of a value, ensuring that the program doesnโ€™t attempt to access inaccessible or undefined memory.
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ cplusplus โ€บ cpp_null_pointers.htm
C++ Null Pointers
The NULL pointer is a constant with a value of zero defined in several standard libraries, including iostream. Consider the following program โˆ’ ยท #include <iostream> using namespace std; int main () { int *ptr = NULL; cout << "The value of ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c++ โ€บ null-pointer-in-cpp
NULL Pointer in C++ - GeeksforGeeks
It is commonly used during variable declaration to show that a pointer is not yet associated with any memory, and it is also returned by many built-in functions to indicate a failure condition.
Published ย  6 days ago
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ cprogramming โ€บ c_null_pointer.htm
NULL Pointer in C
A NULL pointer in C is a pointer that doesn't point to any of the memory locations. The NULL constant is defined in the header files stdio.h, stddef.h as well as stdlib.h. A pointer is initialized to NULL to avoid the unpredicted behavior of a
๐ŸŒ
Eskimo
eskimo.com โ€บ ~scs โ€บ cclass โ€บ notes โ€บ sx10d.html
10.4 Null Pointers
What this means is that no other valid pointer, to any other variable or array cell or anything else, will ever compare equal to a null pointer.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ why am i being asked to set pointer to null after freeing?
r/learnprogramming on Reddit: Why am I being asked to set pointer to Null after freeing?
October 19, 2022 -

During an school excersise one of the requirements is to free and then set to null all mallocs in case of failure.

I fail to understand the logic in this, I thought freeing a variable already "deleted" the information in it, so setting it to Null doesn't seem to be working as a way to delete the memory, right?

And its being asked for when the code fails, like when a malloc fails for one variable I'd have to free and null point the other variables before leaving the program.

Could you help me understand the logic behind this? Thenks!

Edit: thank you so much for everyone that took the time to explain it! Learned alot about houses... I mean frees and Nulls!

๐ŸŒ
Codecademy
codecademy.com โ€บ docs โ€บ pointers โ€บ null pointer
C | Pointers | Null Pointer | Codecademy
February 3, 2025 - It is typically assigned to a pointer to indicate that it is not pointing to any valid memory. #include <stddef.h> pointer_type *pointer_name = NULL;
๐ŸŒ
IBM
ibm.com โ€บ docs โ€บ en โ€บ i โ€บ 7.3.0
Null pointers - IBM Documentation
October 7, 2025 - You can specify any of the following values for a null pointer constant: ... Note: NULL is a macro. It must be defined before use. ... You can use an integer constant expression with the value 0 or an expression that is cast to(void *)0 as a null ...
Top answer
1 of 8
119

Is it possible to initialize a C pointer to NULL?

TL;DR Yes, very much.


The actual claim made on the guide reads like

On the other hand, if you use just the single initial assignment, int *my_int_ptr = 2;, the program will try to fill the contents of the memory location pointed to by my_int_ptr with the value 2. Since my_int_ptr is filled with garbage, it can be any address. [...]

Well, they are wrong, you are right.

For the statement, (ignoring, for now, the fact that pointer to integer conversion is an implementation-defined behaviour)

Copyint * my_int_ptr = 2;

my_int_ptr is a variable (of type pointer to int), it has an address of its own (type: address of pointer to integer), you are storing a value of 2 into that address.

Now, my_int_ptr, being a pointer type, we can say, it points to the value of "type" at the memory location pointed by the value held in my_int_ptr. So, you are essentially assigning the value of the pointer variable, not the value of the memory location pointed to by the pointer.

So, for conclusion

Copy char *x=NULL;

initializes the pointer variable x to NULL, not the value at the memory address pointed to by the pointer.

This is the same as

Copy char *x;
 x = NULL;    

Expansion:

Now, being strictly conforming, a statement like

Copy int * my_int_ptr = 2;

is illegal, as it involves constraint violation. To be clear,

  • my_int_ptr is a pointer variable, type int *
  • an integer constant, 2 has type int, by definition.

and they are not "compatible" types, so this initialization is invalid because it's violating the rules of simple assignment, mentioned in chapter ยง6.5.16.1/P1, described in Lundin's answer.

In case anyone's interested how initialization is linked to simple assignment constraints, quoting C11, chapter ยง6.7.9, P11

The initializer for a scalar shall be a single expression, optionally enclosed in braces. The initial value of the object is that of the expression (after conversion); the same type constraints and conversions as for simple assignment apply, taking the type of the scalar to be the unqualified version of its declared type.

2 of 8
54

The tutorial is wrong. In ISO C, int *my_int_ptr = 2; is an error. In GNU C, it means the same as int *my_int_ptr = (int *)2; . This converts the integer 2 to a memory address, in some fashion as determined by the compiler.

It does not attempt to store anything in the location addressed by that address (if any). If you went on to write *my_int_ptr = 5;, then it would try to store the number 5 in the location addressed by that address.

๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ what is null pointer in c?
What is Null Pointer in C? - Scaler Topics
September 4, 2023 - In the C programming language, a null pointer is a pointer that does not point to any memory location and hence does not hold the address of any variables. It just stores the segment's base address.
๐ŸŒ
Quora
quora.com โ€บ How-do-you-assign-a-value-to-a-pointer-that-points-to-null-C-development
How to assign a value to a pointer that points to null (C++, development) - Quora
Avoid NULL or 0 where possible because nullptr has pointer type. ... Always ensure the target address is valid for the pointerโ€™s type before dereferencing. Avoid dangling pointers: do not assign p = &local_var that goes out of scope after use. After delete, set pointer to nullptr to avoid use-after-free: