In this paragraph we will talk about some common errors that can occur with pointers.
1) Pointers that are not initialized
If a pointer is not initialized, a pointer will point to a random memory location. For example:
If a pointer is not initialized, a pointer will point to a random memory location. For example:
int *A;
*A = 5;
When we declare pointer A, it will point to a random memory address. The pointer could point to memory of program code space, system stack etc, etc. (We don’t know). Then we say *A = 5; the program will try to write the value five at the random location A is pointing to. The program may crash right away, run for an hour and crash, corrupt data or nothing will happen. The result cannot be predicted. So always initialize pointers.
2) Invalid pointer reference
We have two pointers, A and B. A is initialized and B is not. If we say “A=B;”, pointer A points
to a random memory address (remember B is not initialized). Now any reference to *p is now an invalid pointer reference.
We have two pointers, A and B. A is initialized and B is not. If we say “A=B;”, pointer A points
to a random memory address (remember B is not initialized). Now any reference to *p is now an invalid pointer reference.
3) NULL pointers: run-time error and segmentation fault
The NULL pointer has a reserved value in the C and C++ language (very often the value zero but not necessarily). It indicates that it refers to nothing. A NULL pointer should not be confused with an uninitialized pointer. Because it refers to nothing, an attempt to dereference a NULL pointer can cause a run-time error (or often in C programs a segmentation fault).