What are the ways to a null pointer can use in C programming language?
Answers
Null Pointer:
(i) It is a pointer that does not point to any valid memory address.
(ii) It points to the base address of the data segment.
(iii) In simple terms, a pointer holding a null value is called as "null pointer".
Ex: int *ptr = NULL:
Usage of null pointer:
(i) It is helpful in avoiding program crashes when you pass garbage value in your code.
(ii) For freeing the memory when you don't need that data by deleting it.
(iii) In creating linked lists, null pointers are used.
Hope it helps!
To initialize a pointer variable when that pointer variable isn’t assigned any valid memory address yet.
b) To check for null pointer before accessing any pointer variable. By doing so, we can perform error handling in pointer related code e.g. dereference pointer variable only if it’s not NULL.
c) To pass a null pointer to a function argument when we don’t want to pass any valid memory address.