Cursor NULL in C / C ++
It is always a good practice to assign a NULL pointer to a pointer variable in case you don't know the exact address to be assigned. This is done at the time of variable declaration. A pointer that is assigned NULL is called a null pointer .
The NULL pointer is a constant with a value of 0 defined in several standard libraries, including iostream . You consider the following example:
#include using namespace std ; int main () { int * contro = NULL ; cout << "Gia tri cua contro la " << ptr ; return 0 ; }
When the above code is compiled and executed, it gives the following result:
Gia tri cua contro la 0
On most operating systems, programs are not allowed to access memory at address 0, because that memory is reserved by the operating system. However, memory address 0 has special meaning; it signals that the pointer cannot point to an accessible memory location. But by convention, if a pointer contains a value of 0, it is treated as not pointing to anything.
To test a null pointer, you can use an if statement as follows:
if ( contro ) // true neu contro khong la NULL if (! contro ) // true neu contro la NULL
If all unused cursors are provided with null values and you avoid using null pointers, you can avoid the misuse of an uninitialized pointer. Many times, uninitialized variables hold some junk values, and it makes it more difficult to debug the program.
According to Tutorialspoint
Previous article: Cursor in C / C ++
Next lesson: Arithmetic pointer in C / C ++
You should read it
May be interested
- Arithmetic pointer in C / C ++as explained in the main chapter, the cursor in c / c ++ is an address, which is a numeric value. therefore, you can perform arithmetic operations on a pointer as you would with numeric values. there are 4 arithmetic operators that can be used on pointers: ++, -, +, and -.
- Cursor and Array in C ++pointers and arrays are closely related. in fact, pointers and arrays are interchangeable in some cases.
- Array of pointers in C ++before we understand the concept of pointer arrays, we consider the following example, which uses an array of 3 integer numbers.
- Pointers to pointers in C ++a pointer to a cursor is an unidirected form or a string of pointers. typically, a pointer contains the address of a variable.
- Pass cursor to function in C ++c ++ allows you to pass a pointer to a function. to do this, you simply need to declare the function parameter as in a pointer type.
- Returns the pointer from the function in C ++as we have seen how c ++ allows to return an array from a function, similarly, c ++ allows you to return a pointer from a function.