

This is because the null pointer can be integer also. Points to be remembered while working with the NULL functions in c++ see below ġ) We have to assign the null value to the pointer at the time of initiation only.Ģ) If the pointer does not point to any memory address in C++, it does not point to null we will use NULL functions to assign them value.ģ) If we assign a value to a pointer using null functions, then they will convert to Boolean true or false depending on the value they are holding. this is because dereferencing means go back to the previous state where it is pointing to before initiation, but if we try to do this in our code, a null pointer still points nowhere because it has no memory address attached with it. We can also do dereferencing of our null pointers in c++, but this will lead to unusual behavior of the program. Null functions are nothing but a way to assign value to the pointer variable in c++. So in this way, we can test our pointers as well. In the if statement above, as you can see pointer is pointing to null, but here it got converted into Boolean false, and if the value for any of the pointers is not null, then it will convert into Boolean true. So a null can be an integer value as well when it is not pointing to the memory address. If we see it will assign a default value of ‘0’ to the pointer. If the statement coming out to be right, then the print statement will be executed otherwise, it will return. After this, we are making one check here to check and print the value of the pointer. So as we can see, we have initialized the value for the variable at the time of declaring the variables. In this example, we create three different pointers, and all of them point to the NULL here. Now we will see one example and understand its working how it actually works for more detail, see below Ĭout << "demo value for myptr " << myptr1 But we have to use this while initiation of the pointer. Also, we know that pointer holds the memory address, so if we want them to point to some other value, in that case, we can use NULL here. That’s why it is also known as a special value to the pointer. By the use of this, we can give them a logical value when they are not pointing to any address in the memory.


Sample practice syntax for more understanding see below int main () How does the null function work in C++?Īs of now, we know that we use Null functions to assign some special value to the pointer variable. First, we have to define the pointer, and then we can initialize it with NULL. In the above syntax, we are using the NULL value here to assign to a pointer.
