🌐
GeeksforGeeks
geeksforgeeks.org › c language › null-pointer-in-c
NULL Pointer in C - GeeksforGeeks
By doing so, we can perform error ... don’t want to pass any valid memory address. A NULL pointer is used in data structures like trees, linked lists, etc....
Published: January 10, 2025
Discussions

Why is there a NULL in the C language? - Stack Overflow
So 0 qualifies as a null pointer constant, as does (void *)0 and NULL. The use of NULL is preferred however as it makes it more evident to the reader that a null pointer is being used and not the integer value 0. More on stackoverflow.com
🌐 stackoverflow.com
Question: Why are NULL pointers so ridiculously hated?
Looking at it the other way around, what advantage does the use of null provide over an option/result type? More on reddit.com
🌐 r/ProgrammingLanguages
90
0
May 25, 2023
Why do we use Null pointers?
The value where a null pointer is pointing is irrelevant. A null pointer is a pointer that is pointing nowhere--there is no value. In C, the number 0 was defined as the representation of the null pointer. They could have picked any number, but it would have been painful. More on reddit.com
🌐 r/C_Programming
56
0
August 30, 2024
What is Null?
NULL is nothing. It means theres nothing there. Often its just a place holder before something is there. Say u have a bunch of variables of animals. U might set them to NULL before u know what they are. Then u might check if they are NULL before u add an animal to them. Things are initialized as NULL. More on reddit.com
🌐 r/learnprogramming
60
34
July 5, 2024
🌐
TutorialsPoint
tutorialspoint.com › cprogramming › c_null_pointer.htm
NULL Pointer in C
To check for a null pointer before ... variable only if it's not NULL. A NULL pointer is always used to detect the endpoint of trees, linked lists, and other dynamic ......
🌐
Codecademy
codecademy.com › docs › pointers › null pointer
C | Pointers | Null Pointer | Codecademy
February 3, 2025 - ... In C, NULL is often defined as ((void \*)0). While NULL and 0 can be used interchangeably in many contexts, using NULL improves code readability, making it clear that the value is a pointer rather than an integer.
🌐
W3Schools
w3schools.com › c › c_null.php
C NULL
It helps you avoid using pointers that are empty or invalid. You can compare a pointer to NULL to check if it is safe to use.
🌐
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 - A null pointer in C does not point to any memory location. They are used in dynamic memory allocation, error handling, initialization of other pointers and more.
🌐
EDUCBA
educba.com › home › software development › software development tutorials › c programming tutorial › null pointer in c
Null pointer in C | How Null pointer work in C with Examples
March 28, 2023 - Null pointers are used to avoid crashing down of the program: As we saw earlier if we declare any pointer without assigning anything to it then it takes garbage value where it may result in crashing of the system program.
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
Scaler
scaler.com › home › topics › what is null pointer in c?
What is Null Pointer in C? - Scaler Topics
September 4, 2023 - In the example below, We create a pointer *ptr and assign it the value NULL to indicate that it doesn't point to any variables. We add a condition after creating a pointer variable that checks if the value of the pointer is null. ... The built-in malloc() method is used to allocate memory in ...
Find elsewhere
🌐
Uncodemy
uncodemy.com › blog › null-pointer-in-c-and-c-plus-plus-meaning-and-usage
Null Pointer in C and C++: Meaning and Usage
1 month ago - Error Detection: Functions often return null pointers to indicate that something went wrong (e.g., failure in memory allocation). Control Structures: In data structures like linked lists and trees, null pointers are used to mark the end of a list or the absence of a child node.
🌐
CSEstack
csestack.org › home › 3 major use of null pointer in c programming | what does actually null means?
3 Major use of NULL Pointer in C Programming | What does actually NULL means?
March 9, 2019 - Here is a simple C program to print the value of NULL macro. ... We can use this NULL constant value to assign to any pointer so that it will not point to any of the memory locations.
Top answer
1 of 2
7

Actually, you can use a literal 0 anyplace you would use NULL.

Section 6.3.2.3p3 of the C standard states:

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 section 7.19p3 states:

The macros are:

NULL

which expands to an implementation-defined null pointer constant

So 0 qualifies as a null pointer constant, as does (void *)0 and NULL. The use of NULL is preferred however as it makes it more evident to the reader that a null pointer is being used and not the integer value 0.

2 of 2
5

NULL is used to make it clear it is a pointer type.

Ideally, the C implementation would define NULL as ((void *) 0) or something equivalent, and programmers would always use NULL when they want a null pointer constant.

If this is done, then, when a programmer has, for example, an int *x and accidentally writes *x = NULL;, then the compiler can recognize that a mistake has been made, because the left side of = has type int, and the right side has type void *, and this is not a proper combination for assignment.

In contrast, if the programmer accidentally writes *x = 0; instead of x = 0;, then the compiler cannot recognize this mistake, because the left side has type int, and the right side has type int, and that is a valid combination.

Thus, when NULL is defined well and is used, mistakes are detected earlier.

In particular answer to your question “Is there a context in which just plain literal 0 would not work exactly the same?”:

  • In correct code, NULL and 0 may be used interchangeably as null pointer constants.
  • 0 will function as an integer (non-pointer) constant, but NULL might not, depending on how the C implementation defines it.
  • For the purpose of detecting errors, NULL and 0 do not work exactly the same; using NULL with a good definition serves to help detect some mistakes that using 0 does not.

The C standard allows 0 to be used for null pointer constants for historic reasons. However, this is not beneficial except for allowing previously written code to compile in compilers using current C standards. New code should avoid using 0 as a null pointer constant.

🌐
WsCube Tech
wscubetech.com › resources › c-programming › null-pointer
Null Pointer in C Language (Uses, Best Practices, Examples)
July 27, 2026 - Learn in this tutorial about the null pointer in C, including its syntax, uses, how to check it, best practices, and examples to write efficient programs.
🌐
LabEx
labex.io › tutorials › c-using-null-pointer-in-c-programming-123293
Using Null Pointer in C Programming
In C programming, a null pointer is represented by the constant NULL, which is defined in the header file stdio.h. Using a null pointer can help to avoid errors and add functionality to C programs. In this lab, you will learn about null pointers and how to use them in C programming.
🌐
Dot Net Tutorials
dotnettutorials.net › home › null pointer in c
Null Pointer in C Language with Examples - Dot Net Tutorials
November 16, 2023 - It’s a special type of pointer used to indicate that it is not intended to point to an accessible memory location. Using a null pointer is essential for error handling and to avoid undefined behavior caused by uninitialized or dangling pointers.
🌐
Lysator
lysator.liu.se › c › c-faq › c-1.html
Null Pointers
As a matter of style, many people prefer not to have unadorned 0's scattered throughout their programs. For this reason, the preprocessor macro NULL is #defined (by <stdio.h> or <stddef.h>), with value 0 (or (void *)0, about which more later). A programmer who wishes to make explicit the distinction between 0 the integer and 0 the null pointer can then use NULL whenever a null pointer is required.
🌐
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.
🌐
CodeWithHarry
codewithharry.com › tutorial › c-null-pointer
NULL Pointer | C Tutorial | CodeWithHarry
A NULL pointer is used for comparison with other pointers to check whether that other pointer itself is pointing to some memory address or not. We use it for error handling in the case of C programming.
🌐
PiEmbSysTech
piembsystech.com › home › programming languages › null pointer in c language
Null Pointer in C Language - PiEmbSysTech - Embedded Systems & VLSI Lab
September 15, 2023 - Certainly, here are some examples ... for Null Pointers in C Language: Null pointers are commonly used to check if memory allocation was successful: int *ptr = NULL; ptr = (int *)malloc(sizeof(int)); if (ptr != NULL) { // ...
🌐
Eskimo
eskimo.com › ~scs › cclass › notes › sx10d.html
10.4 Null Pointers
You can use a null pointer as a placeholder to remind yourself (or, more importantly, to help your program remember) that a pointer variable does not point anywhere at the moment and that you should not use the ``contents of'' operator on it (that is, you should not try to inspect what it points ...
🌐
Javatpoint
javatpoint.com › null-pointer-in-c
Null Pointer in C - javatpoint
Null Pointer in C with programming examples for beginners and professionals covering concepts, control statements, c array, c pointers, c structures, c union, c strings and more.