Trending September 2023 # How Nullptr Works In C++ With Examples? # Suggested October 2023 # Top 18 Popular | Dacquyenphaidep.com

Trending September 2023 # How Nullptr Works In C++ With Examples? # Suggested October 2023 # Top 18 Popular

You are reading the article How Nullptr Works In C++ With Examples? updated in September 2023 on the website Dacquyenphaidep.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 How Nullptr Works In C++ With Examples?

Introduction to C++ nullptr

Web development, programming languages, Software testing & others

Syntax

The nullptr keyword is utilized for the null values in the pointer variable. It has its own syntax and representations in the C++ programming language. Let’s see the below syntax code.

using namespace std; return type function name(data type* variable name) { } return type main() { function name(NULL); }

Above mentioned codes, we can create the function and passing the pointer arguments. The same function will be called in the main function, and the NULL value will be passed in that function.

How nullptr works in C++?

Generally, the keyword nullptr is denoted using the pointer literals in both variables and functions. It is also called a prvalue of type std::nullptr_t; using these function; we can call the nullptr and the null value as the arguments. Therefore it exists in both implicit and explicit conversations and is mainly pointed out in the implicit conversions from the keyword nullptr into null pointer values is of include any pointer types and also it converts into any pointer to other member types. Similarly, the pointer conversions have existed for any null pointer value constants, and it includes the values of any types like type std::nullptr_t as well as the same in the macro-level values as NULL.

Further, we can check the nullptr in any areas in the coding section, whereas NULL value is expected for more than once in some scenarios. Likewise, the NULL value and the pointer also can be converted into any pointer types, so type conversions are more possible in the nullptr areas. But this is not possible for implicit conversion to the integer type values; it will be the focus on explicit conversion areas for integral level type values. The nullptr has the 0th index of the memory reference when compared to the null value the value specifies void data type is the null also nullptr is only defined using the pointer concept mainly the memory reference of the nullptr variable is 0 also the type is considered using the pointer type. Whereas the Null is the zeroth value of the memory address, the value is considered an integer data type.

Examples of C++ nullptr

Here are the following example mention below

Example #1

Code:

using namespace std; int main() { nullptr_t pi1, pi2, pi3; { cout << “The pointer 1 is greater than the pointer 2 value” << endl; } { cout << “The pointer 2 is greater than the pointer 3 value” << endl; } else { cout << “The pointer 3 is greater than the pointer 2 value” << endl; } char *ch = pi1; if (ch == nullptr) cout << “The ch pointer variable has null value with 0th addresss reference” << endl; else cout << “The ch pointer variable is not null valee it has some value with address reefernce” << endl; return 0; }

Output:

Example #2

Code:

using namespace std; int demo(int ptr1){ cout << “Welcome user your function is demo(int ptr1)” << std::endl; } int demo(char* ch) { cout << “Welcome user your function is demo(char* ch)” << std::endl; } int demo1(int ptrs2) { cout << “Welcome user your function is demo1(int ptrs2)” << std::endl; } int demo1(char* ch2) { cout << “Welcome user your function is demo1(char* ch2)” << std::endl; } int demo2(long ptrs3) { cout << “Welcome user your function is demo2(long ptrs3)” << std::endl; } int demo2(string* sc2) { cout << “Welcome user your function is demo2(string* sc2)” << std::endl; } int demo3(long ptrs4) { cout << “Welcome user your function is demo3(long ptrs4)” << std::endl; } int demo3(string* sc3) { cout << “Welcome user your function is demo3(string* sc3)” << std::endl; } int demo4(int ptrs3) { cout << “Welcome user your function is demo4(int ptrs3)” << std::endl; } int demo4(char* ch4) { cout << “Welcome user your function is demo4(char* ch4)” << std::endl; } int demo5(short ptrs6) { cout << “Welcome user your function is demo5(short ptrs6)” << std::endl; } int demo5(string* sc8) { cout << “Welcome user your function is demo5(string* sc8)” << std::endl; } int main() { cout << demo(NULL) << std::endl; cout << demo1(nullptr) << std::endl; cout << demo2(nullptr) << std::endl; cout << demo3(nullptr) << std::endl; cout << demo4(nullptr) << std::endl; cout << demo5(nullptr) << std::endl; }

Output:

After removing the code i.e.   cout << demo(NULL) << std::endl;

Example #3

Code:

using std::string; int main() { std::cout << std::endl; int p= 0; int* q= 0; auto r= 0; std::cout << typeid(r).name() << std::endl; auto output= p+q+r; std::cout << “output: ” << output << std::endl; std::cout << typeid(output).name() << std::endl; std::cout << std::endl; int  *abs = NULL; std::cout << “The value of abs is null ” << abs ; return 0; }

Output:

In the above three examples, we used nullptr in different scenarios. In the first and third example, we used pointer variables with the value, and we can validate the variable using the nullptr. In the second example, we can compare the null and nullptr difference the null value used in the demo() function it shows error and the function which we called it’s as the nullptr like deom1(nullptr) to demo5(nullptr) it showed the output and also the nullptr memory address reference is displayed on the output screen.

Conclusion

The nullptr referenced variable has no more value in the specific address of the pointer variable in the memory. The reference of the variable is always the 0th index and the variable value is 0 but the index of the variable is always reserved areas in the memory location. We can be called nullptr keywords in the different areas along with the C++ functions.

 Recommended Articles

 This is a guide to C++ nullptr. Here we discuss How nullptr works in C++ and Examples along with the codes and outputs. You may also have a look at the following articles to learn more –

You're reading How Nullptr Works In C++ With Examples?

Update the detailed information about How Nullptr Works In C++ With Examples? on the Dacquyenphaidep.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!