From b9362d867496dc22526cff7d755dfcd1f71fa522 Mon Sep 17 00:00:00 2001 From: hariom Choudhary Date: Sun, 16 Dec 2018 08:26:35 +0530 Subject: [PATCH] Null Pointer explained (#25098) * Null Pointer explained * Grammar and formatting fixes --- guide/english/c/pointers/index.md | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/guide/english/c/pointers/index.md b/guide/english/c/pointers/index.md index bf9c7db151..2d21483d4c 100644 --- a/guide/english/c/pointers/index.md +++ b/guide/english/c/pointers/index.md @@ -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: - # Before you go on... ## A review * Pointers are variables, but instead of storing a value, they store a memory location.