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.

Answer from Gyapti Jain on Stack Overflow
๐ŸŒ
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 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 *

Discussions

How do I return a Null Pointer in a function C++ - Stack Overflow
I am currently working on a bit of code that will search within a vector of type Person (which I have defined in the code and will show if needed). If it finds the person, it returns their name. Th... More on stackoverflow.com
๐ŸŒ stackoverflow.com
pointers - When are you able to return NULL as the returning value of a C function? - Stack Overflow
For instance int lenght() can't return NULL because is is expecting an int in the return statement. But the function struct node* find(int key), when working with linked lists allows me to return NULL. ... Pointer versus not pointer. More on stackoverflow.com
๐ŸŒ stackoverflow.com
C++ return null pointer - Stack Overflow
I have this code below and I think there is something that I don't understand.d_header is a pointer of type WaterHeater and a variable of the class house. Line 2 creates a pointer that points to a More on stackoverflow.com
๐ŸŒ stackoverflow.com
qt - Can I return a NULL-Pointer in a C++ method? - Stack Overflow
While perfectly possible, there is a more general remark I would like to make: Is returning a "naked" pointer like that really necessary? This is C++, not C.... ... @DevSolar There are quite often reasons to return NULL-Pointers. If you like to return an item-pointer from a list of items, but ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
April 26, 2017
๐ŸŒ
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!
๐ŸŒ
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.
๐ŸŒ
Eskimo
eskimo.com โ€บ ~scs โ€บ cclass โ€บ notes โ€บ sx10d.html
10.4 Null Pointers
When we're done with the inner ... we go around 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....
๐ŸŒ
W3Schools
w3schools.com โ€บ c โ€บ c_null.php
C NULL
You can compare a pointer to NULL to check if it is safe to use. Many C functions return NULL when something goes wrong. For example, fopen() returns NULL if a file cannot be opened, and malloc() returns NULL if memory allocation fails.
Find elsewhere
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.

๐ŸŒ
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.
Top answer
1 of 5
3

Since both are pointing to the same object, if either obj or d_header is changed, the change will be reflected in the other.

That is incorrect. If the contents of what they point to is changed, that change can be seen through either pointer but if one of them is changed so that it points to something different, the other will still point to the previous object.

Simple example:

int i = 10;
int j = 20;

int* ptr1 = &i;
int* ptr2 = ptr1;

At this point, both the pointers point to the same object, i. Value of i can be changed by:

  1. Directly by assigning a value to i.

     i = 15;
    
  2. Indirectly by assigning a value to where ptr1 points to.

     *ptr1 = 15;
    
  3. Indirectly by assigning a value to where ptr2 points to.

     *ptr2 = 15;
    

However, you can change where ptr1 points to by using:

ptr1 = &j;

Now, ptr1 points to j but ptr2 still points to i.

Any changes made to i will be visible through ptr2 but not ptr1.
Any changes made to j will be visible through ptr1 but not ptr2.

Isn't obj also pointing to null, since both object were pointing to the same object?

The answer should be clear now. obj continues to point to what d_header used to point to. It is not NULL.

So what is the deal of returning a Null pointer?

The function does not necessarily return a NULL pointer. The function returns whatever d_header used to point to before it was changed to be nullptr. It could be a NULL pointer if d_header used to be NULL before the call to the function.

is nullptr in this case would be the same as delete?

No, it is not. There are two different operations. Assigning a pointer to nullptr does not automatically mean that delete gets called on the pointer. If you need to deallocate memory that a pointer points to, you'll have to call to call delete explicitly.

2 of 5
2

Q: "Isn't obj also pointing to null, since both object were pointing to the same object?"

No, both obj and d_heater contain addresses in memory. Changing one does not change the other. This is easy to see if you think about non-pointer variables:

int foo = 13;
int bar = foo;
foo = 42;

Obviously we know that bar still holds 13, and in the same way obj still holds the original address.

If you want obj to keep the same value as d_heater you can make it a reference, then obj and d_heater are the same variable they don't simply share the same value. We can see this again looking at non-pointer variables, but this time let's make bar a reference:

int foo = 13;
int& bar = foo;
foo = 42;

Now both foo and bar will equal 42. If you want to accomplish the same thing with obj make it a pointer reference:

WaterHeater*& obj = d_heater;

You can see a more detailed example here: http://ideone.com/I8CTba

Q. "Is nullptr in this case would be the same as delete?"

No, again d_heater is only an address. If you assign a new value to d_heater it is simply referencing a different point in memory. If by reassigning d_heater you lose the address of dynamically allocated memory that's very bad, it's known as a memory leak. To prevent leaking you must always release dynamically allocated memory before losing the last address to your dynamically allocated memory (call delete for memory allocated with new.)

That said, use of dynamic memory allocation is best left to the standard libraries, unless you really know what you're doing. So I'd strongly recommend you look at using auto-pointers. In C++11 those are unique_ptr and shared_ptr.

๐ŸŒ
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 - In C programming language pointers are used to point to the memory that is allocated dynamically or at run time and a pointer can be of any data type like int, float, char, etc. In this article, we are discussing the null pointer in C, where NULL is constant with value 0 in C.
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
๐ŸŒ
Coddy.Tech
coddy.tech โ€บ docs โ€บ null pointers
Runnable C Docs: NULL Pointers | Coddy
September 18, 2026 - And setting p = NULL after free turns a use-after-free - which may silently corrupt data - into an immediate, debuggable crash. Return NULL to mean "no result", and say so. // returns a pointer to the matching element, or NULL if there is none ...
๐ŸŒ
Codecademy
codecademy.com โ€บ docs โ€บ pointers โ€บ null pointer
C | Pointers | Null Pointer | Codecademy
February 3, 2025 - Always check for NULL before dereferencing pointers. Use NULL as a return value for functions that encounter errors or failure in memory allocation.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c++ โ€บ null-pointer-in-cpp
NULL Pointer in C++ - GeeksforGeeks
It indicates that the pointer is intentionally set to point to nothing and holds the value NULL (or nullptr in C++11 and later). 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: June 8, 2026
๐ŸŒ
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?

๐ŸŒ
Reddit
reddit.com โ€บ r/cpp_questions โ€บ returning reference of null pointer
r/cpp_questions on Reddit: Returning reference of NULL pointer
July 18, 2021 -

In following function I'm trying find a node in Binary tree that matches the key. I'm passing reference node pointer as smart pointer and this function returns a refernce to node pointer.

How can I return NULL ? As the return value of function is std::unique_ptr<node>& so it is supposed to return a reference.

std::unique_ptr<node>& BST::ReturnNodePrivate(const int& key, std::unique_ptr<node> &ptr){
    if(NULL != ptr){
        if(ptr->key == key){
            return ptr;
         } 
    } 
     else{ 
        return NULL; 
     } 
}

How can I return NULL ?

๐ŸŒ
Dot Net Tutorials
dotnettutorials.net โ€บ home โ€บ null pointer in c
Null Pointer in C Language with Examples - Dot Net Tutorials
November 16, 2023 - In the below example, we use the built-in malloc() function to allocate the memory. If the malloc() function is unable to allocate the memory, then it returns a NULL pointer. Therefore, it is necessary to add the condition to check whether the value of a pointer is null.
๐ŸŒ
Learn C++
learncpp.com โ€บ cpp-tutorial โ€บ null-pointers
12.8 โ€” Null pointers โ€“ Learn C++
August 12, 2015 - Much like the keywords true and false represent Boolean literal values, the nullptr keyword represents a null pointer literal. We can use nullptr to explicitly initialize or assign a pointer a null value.