Difference between Null String and Empty String in C++
When a string in C++ is declared without anything, it is typically initialized as either Null or Empty. Though they may appear to be comparable, they differ in terms of the data they store and refer to. In this article, we will discuss the difference between Null String and Empty String in C++. But before discussing their differences, we must know about the Null String and Empty String.
What is Null String in C++?
A pointer to the NULL character is called a null string. If we want to access or modify it, it will exhibit undefined behavior because it is not pointing to any memory address and does not refer to a valid string. As a result, when using null pointers, we must take the appropriate safety measures.
Syntax:
It has the following syntax:
char *str = NULL;
Example:
Let us take an example to illustrate the Null String in C++.
#include <iostream>
#include <string>
using namespace std;
int main()
{
// Initializing null string
char* str = nullptr; // Using nullptr instead of NULL
// Changing the message and the index accessed attempting to access null string may lead to undefined behavior
cout << "Attempting to access null string..." << endl;
cout << "First character: " << str[3] << endl; // Accessing index 3 instead of 0
return 0;
}
Output:
Attempting to access null string...
Segmentation fault
Explanation:
We are attempting to access a null string, which is not possible and results in undefined behavior in the program mentioned above.
What is Empty String in C++?
A string is initialized as an empty string, meaning that it contains no characters at all. In this instance, the single '\0' NULL character is stored by the char pointer, which also points to the valid memory address.
Syntax:
It has the following syntax:
string emptyStringName = "";
Example:
Let us take an example to illustrate the Empty String in C++.
#include <iostream>
#include <string>
using namespace std;
int main()
{
// Changing the initialization to a string with some content
string nonEmptyStr = "Hello, World!";
// Accessing the size of the non-empty string
cout << "Non-empty String Size: " << nonEmptyStr.size() << endl;
return 0;
}
Output:
Non-empty String Size: 13
Example 2:
Let us take an example to illustrate these Strings in C++.
#include <iostream>
#include <string>
using namespace std;
int main()
{
// Initializing an empty string
string emptyStr = "";
// Accessing empty string safely
cout << "Empty String Size: " << emptyStr.size() << endl;
// Initializing a non-empty string
string nonEmptyStr = "Programming is fun!";
// Accessing the size of the non-empty string
cout << "Non-empty String Size: " << nonEmptyStr.size() << endl;
return 0;
}
Output:
Empty String Size: 0
Non-empty String Size: 19
Explanation:
- Using "", an empty string is initialized.
- The size() member function is used to print the empty string's size.
The text "Programming is fun!" is initialized into a non-empty string - Using the size() member function, the size of the non-empty string is printed.
Key Differences between the Null String and Empty String in C++:
There are several differences between the Null String and Empty String in C++. Some main differences between these strings are as follows:
Feature | Null String | Empty String |
Initialization | Initialized pointer to NULL | Characters absent from the string object created. |
Validity | This string is invalid. | Characters in a valid string object are zero. |
Getting at data | If accessed, it might result in undefinable behavior. | Clearly defined actions on a blank string. |
Crash Hazard | Higher risk because of the possible null pointer. | Reduced risk because the operations are on an authorized object. |
Conclusion:
In conclusion, the differences between an empty string and a null string should be understood when working with strings in C++. Either explicitly using "" or by declaring a string variable and setting it to null, an empty string is a valid string object devoid of characters. When the size() member function is used to access its size, the result is 0. On the other hand, a string pointer that does not point to any string or to any valid memory location is usually referred to as a null string. Typically, nullptr is used to initialize the null pointer that represents it. Undefined behavior may occur when trying to access a null string. Understanding these concepts helps in writing robust and error-free C++ code, ensuring proper handling and manipulation of strings throughout the program.