By the use of this, we can give them a logical value when they are not pointing to any address in the memory. 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;. 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. 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. A pointer that is assigned NULL is called a null pointer. The NULL pointer is a constant with a value of zero defined in several standard libraries, including iostream. On most of the operating systems, programs are not permitted to access memory at address 0 because that memory is reserved by the operating system.
However, the memory address 0 has special significance; it signals that the pointer is not intended to point to an accessible memory location. But by convention, if a pointer contains the null zero value, it is assumed to point to nothing. Thus, if all unused pointers are given the null value and you avoid the use of a null pointer, you can avoid the accidental misuse of an uninitialized pointer.
Improve this question. Vijay Vijay 1, 4 4 gold badges 23 23 silver badges 33 33 bronze badges. This seems like a statement rather than a question. What are you asking? Add a comment. Active Oldest Votes.
Improve this answer. Community Bot 1 1 1 silver badge. Nawaz Nawaz k gold badges silver badges bronze badges. Writing NULL is semantically richer: it makes it immediately clear that the value is a pointer rather than a mathematical quantity. The two are very different, despite both being represented by machine integers.
Not sure though. Alex Deem Alex Deem 4, 1 1 gold badge 20 20 silver badges 23 23 bronze badges. Jonathan Wood Jonathan Wood Well that could be solved quite easily - if there no initialization, the compiler automatically makes one, i.
Mephane: What if you want it initialized later? I really don't consider this a problem to be solved. Greater performance is achieved when the compiler automatically generates less code.
This behavior has worked well for me. If you want a variable initialized, you initialize it. What's the problem with automatic initialization to 0 and your late initialization actually being an assignment? It's not like you overwrite a useful value, but if initially 0 the pointer is at least in a defined state.
0コメント