Updated April 3, 2023
Introduction to C++ null
The null function is used to assign value to the variable; this can be treated as the default value for the variable defined in many programming languages. Null functions can be used to assign value to a pointer that is not pointing to any address and contain any garbage value, so the null function will assign them a special value called ‘NULL’, which means they are now null pointer. In this topic, we are going to learn about C++ null.
Syntax
This is very simple to assign a null value to the variable in C++; we just need to do this at the time of initialization only. This variable then turns to be treated as the Null pointer. Below see the syntax to understand this better and used while programming see below;
int main () {
int *your_ptr_name = NULL;
}
In the above syntax, we are using the NULL value here to assign to a pointer. First, we have to define the pointer, and then we can initialize it with NULL. Sample practice syntax for more understanding see below;
int main () {
int *myptr = NULL;
// logic goes here .
}
How does the null function work in C++?
As of now, we know that we use Null functions to assign some special value to the pointer variable. By the use of this, we can give them a logical value when they are not pointing to any address in the memory. That’s why it is also known as a special value to 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. But we have to use this while initiation of the pointer. Now we will see one example and understand its working how it actually works; for more detail, see below;
Example:
#include <iostream>
using namespace std;
int main () {
int *myptr1 = NULL;
int *myptr2= NULL;
int *myptr3 = NULL;
if(!myptr1) {
cout << "demo value for myptr " << myptr1 ;
}
return 0;
}
In this example, we create three different pointers, and all of them point to the NULL here. So as we can see, we have initialized the value for the variable at the time of declaring the variables. After this, we are making one check here to check and print the value of the pointer. If the statement coming out to be right, then the print statement will be executed; otherwise, it will return. If we see it will assign a default value of ‘0’ to the pointer. So a null can be an integer value as well when it is not pointing to the memory address. 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 in this way, we can test our pointers as well. Null functions are nothing but a way to assign value to the pointer variable in c++. We can also do dereferencing of our null pointers in c++, but this will lead to unusual behavior of the program. 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.
Points to be remembered while working with the NULL functions in c++ see below;
1) We have to assign the null value to the pointer at the time of initiation only.
2) 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.
3) 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 the null pointer can be integer also.
Examples of C++ null
Given below are the examples of C++ null:
Example #1
In this example, we will see how to initialize the null value to the pointer using the NULL function in C++; this is nothing but the special value we can assign at the time of initialization. There is no particular syntax to do this.
Code:
#include <iostream>
using namespace std;
int main () {
cout<<"Demo for null functions in c++";
cout<<"\n";
// assiging null values heree ..
int *myptr1 = NULL;
int *myptr2= NULL;
int *myptr3 = NULL;
//printing values here
cout << "value of the first variabel is::: " << myptr1 ;
cout<<"\n";
cout << "value of the second variabel is::: " << myptr2 ;
cout<<"\n";
cout << "value of the third variabel is::: " << myptr3 ;
return 0;
}
Output:
Example #2
In this example, we are going to see how to make a conditional statement while using a NULL pointer in your program and how they change the value while checking them. After the statement, we are assigning them a new value to the point.
Code:
#include <iostream>
using namespace std;
int main () {
int var1 =20;
int var2 =30;
int var3 =40;
cout<<"Demo for null functions in c++";
cout<<"\n";
// assigning null values here.
int *myptr1 = NULL;
int *myptr2= NULL;
int *myptr3 = NULL;
//printing values here
cout<<"Value before null functions :::";
cout<<"\n";
cout << "value of the first variable is before ::: " << myptr1 ;
cout<<"\n";
cout << "value of the second variable is before :::" << myptr2 ;
cout<<"\n";
cout << "value of the third variable is before :::" << myptr3 ;
if(!myptr1){
myptr1 = &var1;
cout << "value after initialization is ::" ;
cout<<"\n";
cout << "value of the first variable is after ::: " << myptr1 ;
cout<<"\n";
}
if(!myptr2){
myptr2 = &var2;
cout << "value after initialization is ::" ;
cout<<"\n";
cout << "value of the second variable is after ::: " << myptr2 ;
cout<<"\n";
}
if(!myptr3){
myptr3 = &var3;
cout << "value after initialization is ::" ;
cout<<"\n";
cout << "value of the third variable is after ::: " << 3 ;
cout<<"\n";
}
return 0;
}
Output:
Conclusion
Hence we can use null functions to assign value to the variable; null values are important when our pointer is not pointing to any memory address to avoid the unusual behavior while programming, so null functions or null assigning to a pointer is used to assign a default value when they are not pointing anywhere in the memory address.
Recommended Articles
This is a guide to C++ null. Here we discuss how the null function works in C++ and Examples along with the codes and outputs. You may also have a look at the following articles to learn more –