in Technology by
What is the usage of the NULL pointer in C?

1 Answer

0 votes
by

There is a lot of usage of the null pointers in C but here I am mentioning some important usage of the null pointer in C which you must know.

  • A pointer that is not pointing the address of a valid object or valid memory should be initialized to NULL. It prevents the pointer to become a dangling pointer and ensure the programmer that pointer is not pointing anywhere.
char *pcData = NULL; //Prevent to become dangling pointer
  • A very good habit to check the validity of the pointers before using it. It prevents the crashing of the code and unwanted result. The null pointer helps you in error handling.
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *piData = NULL;
piData = malloc(sizeof(int)*10);
if(NULL == piData)
{
//exit
}
else
{
//code
free(piData); //free after the use
}
return 0;
}
  • There is a lot of library function in C where pointer arguments are optional. So passing the null pointer to a function argument is useful when you don’t want to pass any valid memory or object address.

For example,

int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg);
  • As a summary, you can understand that a null pointer can be used as an error value, a sentinel value or to terminate indirection in the recursive data structure.

Related questions

0 votes
    What is the usage of the pointer in C?...
asked Jan 21, 2021 in Technology by JackTerrance
+1 vote
    What is NULL Pointer in C Programming?...
asked Nov 9, 2020 in Technology by JackTerrance
+1 vote
    What are the ways to a null pointer that can be used in the C programming language?...
asked Nov 8, 2020 in Technology by JackTerrance
0 votes
    What is the difference between an uninitialized pointer and a null pointer?...
asked Jan 21, 2021 in Technology by JackTerrance
0 votes
    What is a NULL pointer?...
asked Jan 17, 2021 in Technology by JackTerrance
0 votes
    According to ISO C++, dereferencing a null pointer is undefined behaviour. My curiosity is, why? Why ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 24, 2022 in Education by JackTerrance
0 votes
    What is (void*)0? Error re of null pointer re of void pointer none of the listed options? Select the correct answer from above options...
asked Nov 30, 2021 in Education by JackTerrance
0 votes
    Describe the header file and its usage in C programming?...
asked Nov 8, 2020 in Technology by JackTerrance
0 votes
    What features of C++ should be avoided in embedded systems? Please classify the answer by reason such as: ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 20, 2022 in Education by JackTerrance
0 votes
    2. Which of the following symbols does the formula usage in Calc start with? a. : (Colon) b. = (Equal to) C. ; (Semi colon) I Select the correct answer from above options...
asked Dec 2, 2021 in Education by JackTerrance
0 votes
    Given the usage of the following tags in HTML. (a) center (b) html (c) p (d) br Select the correct answer from above options...
asked Nov 29, 2021 in Education by JackTerrance
0 votes
    What is Indirection and Increment/Decrement operators with a pointer in C?...
asked Jan 24, 2021 in Technology by JackTerrance
0 votes
    How can we do Pointer comparison in C?...
asked Jan 24, 2021 in Technology by JackTerrance
0 votes
    How is a Program increment a pointer in C?...
asked Jan 24, 2021 in Technology by JackTerrance
0 votes
    Which type of pointer is the most convenient way of storing the raw address in C programming?...
asked Jan 23, 2021 in Technology by JackTerrance
...