what does the * mean in
int* p = nullptr;
Also, it would be helpful if anyone can example what nullptr means. Is it equivalent to null? Sorry, I recently started learning c++;
what does the * mean in
int* p = nullptr;
Also, it would be helpful if anyone can example what nullptr means. Is it equivalent to null? Sorry, I recently started learning c++;
In C and C++, it will be easy if you consider *
as "value at address" operator. Its counterpart &
is "address of" operator. Now let's see how to interpret this:
int* p = nullptr;
One can read this as "Value at address stored in p
is an int
and that address is initialized with nullptr
".
Before C++/11, we used to assign 0
or NULL
to it. NULL
is also defined as 0
. But it could be confused with number 0
that is why nullptr
was introduced. It is equivalent of null
in other languages. For details See: nullptr
what does the * mean in
int* p = nullptr;
*
refers to a pointer-to-an object which holds a specific location/address in memory, in your case it is a pointer-to-an-int and so it refers to the address of an integer.
Also, it would be helpful if anyone can example what nullptr means. Is it equivalent to null? Sorry, I recently started learning c++;
Both NULL
and nullptr
point to the zeroth address (0x000000) in memory.
In most cases, the C++11 keyword nullptr
is similar to NULL
in the sense that they usually point to the same thing. But, there are some subtle differences between the two:
decltype(NULL); /* 'void*' or `intptr_t`, depends on implementation, the former is
* an object pointer type while the latter is just an integer that
* can hold an address */
decltype(nullptr); // Always 'std::nullptr_t'
From this, we find that nullptr
is in fact, not a pointer but an instance of a class, std::nullptr_t
.
Essentially, std::nullptr_t
is a wrapper class which indirectly refers to the zeroth address in memory. All instances of this class (including nullptr
) are implicitly convertible to the pointer of any type.
The reason for this sort of design was to facilitate function overloading, so that nullptr
could have a consistent type that could be safely overloaded, unlike NULL
which does not have a consistent type across different implementations:
#include <iostream>
#include <cstddef>
void f(long int) {
std::cout << "Encountered a long" << std::endl;
}
void f(decltype(NULL)) {
std::cout << "Encountered NULL" << std::endl;
}
void f(decltype(nullptr)) {
std::cout << "Encountered nullptr" << std::endl;
}
int main() {
f(0l);
f(NULL);
f(nullptr);
}
In this case, normally, one would think that this would output:
Encountered a long
Encountered NULL
Encountered nullptr
But it gets more complicated than this, on certain implementations, the compiler would give this error:
// Output from the GCC compiler:
source>:8:6: error: redefinition of 'void f(long int)'
8 | void f(decltype(NULL)) {
| ^
This is because decltype(NULL)
is long int
in this case (not always), which causes the overload void f(long int)
to be defined twice, leading to a compilation error.
This is one of the use cases of nullptr
. It safely retains its type, which is std::nullptr_t
.
In contrast, NULL
doesn't have any defined universal type, so it can't be safely overloaded in a function based on its type.
This and this should be helpful
It's basically used to indicate an object handle, interior pointer, or native pointer type does not point to an object.
int* p = nullptr means the integer pointer that doesn't point to an integer variable