Null Pointer explained (#25098)

* Null Pointer explained

* Grammar and formatting fixes
This commit is contained in:
hariom Choudhary
2018-12-16 08:26:35 +05:30
committed by Manish Giri
parent 0f05bb97bc
commit b9362d8674

View File

@@ -249,6 +249,19 @@ int main(void)
}
```
## Null Pointer
Consider following line
```c
int *p;
```
We have created a pointer which contain garbage value. If we try to dereference it, we will read the value stored at the garbage address and this can lead to unexpected results, such as segmentation fault. Hence we should never leave a pointer uninitialized and instead initialize it to NULL, to avoid unexpected results.
```c
int *p = NULL; // NULL is a constant with value 0
int *q = 0; // same as above
```
### Void Pointer
A void pointer is a pointer variable declared using the reserved word in C void.
Lets illustrate this with a void pointer declaration below:
@@ -296,7 +309,6 @@ Example:
```
Credits: <http://www.circuitstoday.com/void-pointers-in-c>
# Before you go on...
## A review
* Pointers are variables, but instead of storing a value, they store a memory location.