NULL is a pointer value - or rather a null-pointer value.

NULL means that the function can't find where your pointer should point to - for example if you want to open a file, but it doesn't work your file pointer is returned as NULL. So you can test the value of a pointer and check to see if it worked or not.

If you are writing a routine

int length()

then you could return a negative value if length is unable to read the length of whatever you send it - this would be a way of indicating an error, because normally lengths can never be negative....

Answer from tom on Stack Overflow
๐ŸŒ
W3Schools
w3schools.com โ€บ c โ€บ c_null.php
C NULL
#include <stdio.h> #include <stdlib.h> int main() { int *numbers = (int*) malloc(100000000000000 * sizeof(int)); if (numbers == NULL) { printf("Memory allocation failed.\n"); return 1; } printf("Memory allocation successful!\n"); free(numbers); numbers = NULL; return 0; } ... Tip: Always check if a pointer is NULL before using it. This helps avoid crashes caused by accessing invalid memory. ... Coding fundamentals as a game. Bite-sized lessons and challenges. ... Ready to start your journey?
๐ŸŒ
Quora
quora.com โ€บ How-do-you-return-a-null-pointer-in-C
How to return a null pointer in C - Quora
Answer (1 of 7): โ€œHow do you return a null pointer in C?โ€ This will do it: [code]return (void*)0; [/code]Enjoy your null pointer!
Discussions

pointers - When are you able to return NULL as the returning value of a C function? - Stack Overflow
I was wondering if you could tell me when you are able to return NULL, as the result of a function in C. For instance int lenght() can't return NULL because is is expecting an int in the return More on stackoverflow.com
๐ŸŒ stackoverflow.com
c++ - returning NULL pointer in C - Stack Overflow
I had some different answers on this question before, so decided to ask again here. Suppose I have a function node* foo() and if some fail accured, I do return NULL. Does this code really return N... More on stackoverflow.com
๐ŸŒ stackoverflow.com
string - Returning NULL value from a function to a pointer in C - Stack Overflow
However, in skipWords() I am supposed to return a pointer value of NULL if the amount of words that you wish to skip is greater than the amount of words in the string you input. More on stackoverflow.com
๐ŸŒ stackoverflow.com
December 14, 2017
Can you return NULL from a function that returns a multidimensional ***pointer?
NULL is a pointer that is guaranteed by the standard to be non-equal to any other pointer that maps to a valid data. This includes other pointers because pointers are objects as well. So you can safely return NULL of type char*** as a valid value for a pointer to non-existing object of type char**. It is a common practice to return from a function as sentinel indicting that the function had failed. Examples of such a standard functions are fopen, malloc, realloc, etc. And one more thing. The char*** is NOT a multidimensional array. The char[2][3][4] is. While char(*)[2][3][4] is a pointer to such an array More on reddit.com
๐ŸŒ r/C_Programming
21
12
January 17, 2022
๐ŸŒ
W3schools
w3schools.tech โ€บ tutorial โ€บ cprogramming โ€บ c_null_pointer
NULL Pointer in C - Pointers in C - W3schools
In this example, we're using malloc() to allocate memory. If the allocation fails, malloc() returns NULL. We check for this to handle the error gracefully. NULL pointers are also used when working with files.
๐ŸŒ
Eskimo
eskimo.com โ€บ ~scs โ€บ cclass โ€บ notes โ€บ sx10d.html
10.4 Null Pointers
When we're done with the inner ... the outer loop again, to try another starting position. If we run out of those (if *start == '\0'), without finding a match, we return a null pointer....
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ null-pointer-in-c
NULL Pointer in C - GeeksforGeeks
By specifically mentioning the NULL pointer, the C standard gives a mechanism using which a C programmer can check whether a given pointer is legitimate or not. The malloc() function returns the NULL pointer when the memory allocation is failed.
Published: January 10, 2025
Top answer
1 of 5
5

NULL is a pointer value - or rather a null-pointer value.

NULL means that the function can't find where your pointer should point to - for example if you want to open a file, but it doesn't work your file pointer is returned as NULL. So you can test the value of a pointer and check to see if it worked or not.

If you are writing a routine

int length()

then you could return a negative value if length is unable to read the length of whatever you send it - this would be a way of indicating an error, because normally lengths can never be negative....

2 of 5
3

It is a matter of convention and you should clearly have one in your head and document it (at least in comments).

Sometimes a pointer really should always point to a valid address (see this intSwap example, both arguments should be valid pointers). At other times, it should either be such a valid address, or be NULL. Conceptually the pointer type is then by convention a sum type (between genuine pointer addresses and the special NULL value).

Notice that the C language does not have a type (or a notation) which enforces that some given pointer is always valid and non-null. BTW, with GCC specifically, you can annotate a function with __attribute__ using nonnull to express that a given argument is never null.

A typical example is FILE* pointers in <stdio.h>. The fopen function is documented to be able to return NULL (on failure), or some valid pointer. But the fprintf function is expecting a valid pointer (and passing NULL to it as the first argument is some undefined behavior, often a segmentation fault; and UB is really bad).

Some non-portable programs even use several "special" pointer values (which should not be dereferenced), e.g. (on Linux/x86-64) #define SPECIAL_SLOT (void*)((intptr_t)-1) (which we know that on Linux it is never a valid address). Then we could have the convention that a pointer is a valid pointer to a valid memory zone, or NULL or SPECIAL_SLOT (hence, if seen as an abstract data type, it is a sum type of two distinct invalid pointers NULL and SPECIAL_SLOT and the set of valid addresses). Another example is MAP_FAILURE as result of mmap(2) on Linux.

BTW, when using pointers in C to heap allocated data (indirectly obtained with malloc), you also need conventions about who is in charge of releasing the data (by using free, often thru a supplied function to free a data and all its internal stuff).

Good C programming requires many explicit conventions regarding pointers, and it is essential to understand them precisely and document them well. Look for example[s] into GTK. Read also about restrict.

Top answer
1 of 4
13

NULL is a pointer literal which is defined to contain a special value.

One possible definition is:

#define NULL ((void *)0)

For more detail you can read this faq

About const string& foo(), I believe you mean C++'s std::string. std::string has no implicit constructor that initialize it with NULL pointer. So you should use some exception or empty string to indicate an error to the caller. (If you are not throwing, you must return an std::string. Even if the object returned is local, its life is prolonged when kept in a local constant reference. But returning some other type and expecting an implicit conversion is not a good idea.)

Answer to your question: Because NULL is a literal, no temporary object may be created most of the time and actual value can be directly returned to the caller.

2 of 4
3

This function

const string& foo(); 

does not return a pointer. It returns a constant reference to an object of type std::string. So its return value may not be assigned to a pointer.

According to the C++ Standard

4.10 Pointer conversions [conv.ptr]

1 A null pointer constant is an integer literal (2.14.2) with value zero or a prvalue of type std::nullptr_t. A null pointer constant can be converted to a pointer type; the result is the null pointer value of that type and is distinguishable from every other value of object pointer or function pointer type. Such a conversion is called a null pointer conversion. Two null pointer values of the same type shall compare equal. The conversion of a null pointer constant to a pointer to cv-qualified type is a single conversion, and not the sequence of a pointer conversion followed by a qualification conversion (4.4). A null pointer constant of integral type can be converted to a prvalue of type std::nullptr_t. [ Note: The resulting prvalue is not a null pointer value. โ€”end note ]

So when you use null pointer constant defined with macro NULL it is assigned to the return pointer of the function that will contain null pointer value of type node *

Find elsewhere
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
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 - Memory Allocation Failures: Functions like malloc(), calloc(), and realloc() return NULL if they fail to allocate the requested memory. Initializing Pointers: Always initialize pointers to NULL when declaring them if you don't have a valid memory address to assign immediately.
๐ŸŒ
Reddit
reddit.com โ€บ r/c_programming โ€บ can you return null from a function that returns a multidimensional ***pointer?
r/C_Programming on Reddit: Can you return NULL from a function that returns a multidimensional ***pointer?
January 17, 2022 -

I am very sure that someone told me once that NULL is defined as a pointer to void. I leafed through the K&R, and NULL was just said to be interchangeable with zero.

But either way, if I have a function that returns a 3d char array (array of arrays of strings), can I then return NULL if something goes wrong? will it be a valid return type?

Many functions that return pointers return NULL if something goes wrong - fopen for instance where you check for NULL and then perror.

But I am confused about multi-dimensional pointers.

I mean, I know that they are technically just pointers. I am unsure what the multiple asterisks do except tell the programmer how many dimensions there are. Hmmm Is this the solution?

Comments?

๐ŸŒ
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.
๐ŸŒ
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 - Guide to Null pointer in C. Here we discuss how Null pointer work in C with syntax and examples to implement with proper codes and outputs.
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ cprogramming โ€บ c_null_pointer.htm
NULL Pointer in C
The malloc() and calloc() functions are used to dynamically allocate a block of memory. On success, these functions return the pointer to the allocated block; whereas on failure, they return NULL.
๐ŸŒ
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.
๐ŸŒ
GNU
gnu.org โ€บ software โ€บ libc โ€บ manual โ€บ html_node โ€บ Null-Pointer-Constant.html
Null Pointer Constant (The GNU C Library)
Next: Important Data Types, Previous: Variadic Functions, Up: C Language Facilities in the Library [Contents][Index] ยท The null pointer constant is guaranteed not to point to any real object. You can assign it to any pointer variable since it has type void *. The preferred way to write a null ...