NULL, nullptr, 0,’ \0′,’0
NULL, nullptr,’ \0′,’0′,’0′, they all look different, but in fact the first four zeros are different, only the last one is different. Let’s take a look
0
This is just a number zero, a constant, and the ACsill code is zero.
printf("%d\n".'\ 0'); The results of0
Copy the code
‘\ 0’
This is the end of the string, which is actually the number zero, and this is the number zero in character form, which is what you think of as a string.
‘0’
This is the character 0, ASCII code 48, which is completely different from the number 0, not to be confused.
printf("%d\n".'0'); The results of48
Copy the code
NULL
This is implicitly converted to a pointer to the number 0, so you can see that an rvalue is a pointer type, so why don’t you just assign it to 0, because if you assign it to a direct value 0, an lvalue is an integer, an rvalue is a pointer type, and the compiler will warn you, so the direct macro defines NULL, and 0 is implicitly converted to a pointer. And NULL is a better read than 0, so assignments of 0 and NULL work the same way, but when you see 0, you wonder why. When you see NULL, you immediately know that the assignment is NULL.
#define NULL ((void *)0)
Copy the code
nullptr
This is essentially 0, but it is a pointer type 0 in c++. Why not use NULL? Let’s look at the definition of NULL in c++
#ifndef NULL
#ifdef _cpluscplus
#define NULL 0
#else
#define NULL ((void*)0)
#endif
#endif
Copy the code
Can see NULL in c + + is 0, no implicitly converted to a pointer type, this is because the c + + is a strongly typed language, does not support an implicit conversion, although with the effect of the number 0 is consistent, but using a NULL value to a pointer type, in as a parameter, just have a function overloading, according to the shape of a type is int, another for int *, If you want to call a function of type pointer and pass NULL, you will find that the function whose parameter is int is called. So c++11 uses nullptr as the new pointer type 0 instead of NULL. Nullptr is a member of the nullptr_t class template that supports Pointers of any type. In c++, it is best to use nullptr as a pointer to a nullptr.