C++

Null in C++

A null pointer points to nothing. The default constant null has a value of 0. Similar to how C++ end strings, the character 0 is used to end strings. Null is another possible value for a pointer, and unless the CPU supports a particular bit pattern for null pointers, it has the same meaning as zero. Something has no value when marked with the SQL special marker null or referred to as NULL. Frequently used as a separator, terminator, or filler is the zero-valued ASCII character known as “null” or “NUL”. This symbol has no visible representation. An object pointer (or reference) not currently set to point to an object is known as a null pointer or reference. It may also be written as None, NULL, or nil.

Why Do We Utilize Null?

Use of null pointers is permitted in the following circumstances:

Initialize pointers, and illustrate situations similar to the end of an arbitrary-length list. Describe any error with a returned pointer from a function.

Syntax
In C++, it’s relatively easy to give a variable a null value; we only need to do this once, during initialization. This variable is then handled to act as the Null pointer.

Since we are aware that the pointer contains the memory address, we can use NULL in this situation to make the pointer point a different value. However, we must use this when launching the pointer. This article will examine different examples of using the null in C++ programs.

Example No. 1

Three separate pointers are created in this example, and they all point to the NULL. As a result, we can see that when we declared the variables, the variable’s value was initialized. Hereafter, we perform one check to ascertain and print the pointer’s value. If the statement proves accurate, the print statement will run; if not, it will return.

If a pointer in C++ does not access any memory addresses, it does not access null. We will use NULL techniques to give them values.

#include <iostream>
using namespace std;
int main () {
int  *ptr_1 = NULL;
int  *ptr_2= NULL;
int  *ptr_3 = NULL;
if(!ptr_1) {
cout << "Value of the pointer " << ptr_1 ;
}
return 0;
}

In this example, first, we include the library <iostream> and then use the standard namespace. We will call it in the main() function. The next line includes the declaration of three pointers, and we assigned them the value “NULL”. Then, we use the “if” condition. If the required condition is fulfilled, the “cout” statement prints the value of the first pointer.

Example No. 2

In this illustration, we’ll use the C++ NULL function to demonstrate how to initialize the pointer with the null value, simply a particular value we can assign during initialization.

#include <iostream>
using namespace std;
int main () {
cout<<"Use of null functions";
cout<<"\n";

int  *p_1 = NULL;
int  *p_2 = NULL;
int  *p_3 = NULL;

cout << "The calculated value of the first pointer: " << p_1 ;
cout<<"\n";
cout << "The calculated value of the second pointer: " << p_2 ;
cout<<"\n";
cout << "The calculated value of the third pointer: " << p_3 ;
return 0;
}

We have integrated the header file <iostream>. Then we do coding in the body of the main() function. Within the body of the main() function. We initialize three pointers and set these pointers to the “NULL”. By using the “cout” statement, we print the values of the pointers (p_1, p_2, and p_3). In the end, we will enter “return0”.

Example No. 3

Using a NULL pointer to generate a conditional statement in the program and demonstrating how they change the value while initialization is demonstrated in this instance.

#include <iostream>
using namespace std;
int main () {
int v_1 =45;
int v_2 =38;
int v_3 =23;
cout<<"Use of null functions:";
cout<<"\n";
int  *pt1 = NULL;
int  *pt2 = NULL;
int  *pt3 = NULL;
cout<<"The values of variables before using null function:";
cout<<"\n";
cout << "The value of first variable before using null function:" << pt1 ;
cout<<"\n";
cout << "The value of second variable before using null function:" << pt2 ;
cout<<"\n";
cout << "The value of third variable before using null function:" << pt3 ;
cout<<"\n";
if(!pt1){
pt1 = &v_1;
cout << "The value of first variable after initialization: " << pt1 ;
cout<<"\n";
}
if(!pt2){
pt2 = &v_2;
cout << "The value of second variable after initialization: " << pt2 ;
cout<<"\n";
}
if(!pt3){
pt3 = &v_3;
cout << "The value of third variable after initialization: " << pt3 ;
cout<<"\n";
}
return 0;
}

After incorporating the library and standard function, we will invoke the main() function. Then we initialize three variables named v_1, v_2, and v_3. Then, we generate a conditional statement in this code, initialize the values of the pointers, and assign a “NULL” value to every pointer. We have been using the “if” condition. First, we display the value of pointers before using the null function. Then we get the pointer’s values after initializing by using the “cout” statement.

Example No. 4

Use null if you are unable to provide a destination for a pointer. Applications that use pointers are protected against crashes and memory leaks by the null value. Pointer in C and C++ refers to storing a memory address. Always assign the pointer NULL to a pointer variable if you are unsure of the precise location to assign. This happens when a variable is declared. Pointers with the value NULL are considered null pointers.

#include <iostream>
using namespace std;
int main()
{
  int *p = NULL;
  cout << "The value of pointer is " << p ;
  return 0;
}

We will include the <iostream> library at the commencement of the code. Here, we will declare the pointer and assign it with a “NULL” value. Before entering the “return 0” command, the “cout” statement is applied to show the pointer’s value.

Example No. 5

Each variable represents a memory location, and each memory location has a unique address that may be accessed using the ampersand (&) operator, which stands for a memory address. Consider the following, which prints the variable addresses that were declared:

#include <iostream>
using namespace std;
int main ()
{
   int  v1;
   char v2[42];

   cout << "The address of first required variable: ";
   cout << &v1 << endl;

   cout << "The address of second required variable: ";
   cout << &v2 << endl;

   return 0;
}

At the start of the code, we will include the required library. Here, we will initialize a variable “v1” having an integer data type. Another variable, “v2”, will be initialized with a character data type. And this variable is declared as an array. We want to print the address of both these variables, so we will utilize the “cout” statement. The address will be obtained with the help out of the “&” operator.

Conclusion

After reading this article, we see how to use null functions to assign values to variables. When programming, null values are essential for preventing unexpected failures. Therefore when a pointer isn’t referring to any memory addresses, the null functions or null assignments to a pointer are used to provide a default value.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.