NULL Pointer In C [Explained With Examples] – CsTutorialpoint
Hello friends, in today’s article we are going to talk about NULL Pointer In C Language
Today we will learn in detail about, what is NULL Pointer In C and why and how they are used in C language.
So without wasting time let’s first understand what is NULL Pointer In C
What is NULL Pointer In C
In C language, when we do not have any address to assign to a pointer variable, then we assign that pointer variable with NULL.
NULL is a keyword which means that now the pointer is not pointing to anything and when the pointer is not pointing to anything then such pointer is called NULL Pointer.
We can also say that “a NULL pointer is a pointer that is not pointing to nothing.” NULL is a constant whose value is zero (0). We can create a NULL Pointer by assigning NULL or zero (0) to the pointer variable.
Syntax -:
data_type pointer_name = NULL;
- data_type -: any data type can come here like int, char, float, etc.
- pointer_name -: Pointer name you can keep anything according to you.
- NULL -: Here NULL is a keyword which we assign to pointer variable to make NULL Pointer.
Example -:
int *ptr = NULL;
Here ptr is a NULL pointer.
Let’s understand NULL Pointer better through a program.
Example Program of Null pointer
#include <stdio.h>
int main()
{
int number = 20;
int *ptr1 = &number;
int *ptr2;
int *ptr3=0;
int *ptr4=NULL;
if(ptr1 == 0)
printf("ptr1: NULL\n");
else
printf("ptr1: NOT NULL\n");
if(ptr2 == 0)
printf("ptr2: NULL\n");
else
printf("ptr2: NOT NULL\n");
if(ptr3 == 0)
printf("ptr3: NULL\n");
else
printf("ptr3: NOT NULL\n");
if(ptr4 == 0)
printf("ptr2: NULL\n");
else
printf("ptr2: NOT NULL\n");
return 0;
}
Check out this program, In this program, we have declared four pointer variables, out of which we have assigned the first pointer (ptr1) to the address of one variable and we have left the second pointer (ptr2) as declared without assigning anything.
We have assigned the third pointer (ptr3) with zero (0) and assigned the fourth pointer with NULL. And as we know, assigning any pointer to zero or NULL becomes a NULL pointer, so ptr3 and ptr4 is a NULL pointer and ptr1 and pt2 are not a NULL pointer.
Output -:
ptr1: NOT NULL
ptr2: NOT NULL
ptr3: NULL
ptr4: NULL
Some important points of the NULL pointer
- If we compare a null pointer to a pointer that is pointing to an object or function, then this comparison will be unequal.
- In C language, we can compare two null pointers of any type because they are both equal.
- In C language, NULL pointers cannot be dereferenced. If you try to do this then there will be a segmentation fault.
- According to the C standard, 0 is a null pointer constant. example -: “int *ptr = 0;” Here “ptr” is a null pointer.
- NULL vs Void Pointer -: NULL is a value in a null pointer and Void is a type in a void pointer.
Use of null pointer in C
- When a pointer does not point to any valid address, then such pointer becomes a dangling pointer. By assigning NULL to such pointer, we can prevent it from becoming a dangling pointer.
- The null pointer is used in error handling.
Conclusion
Friends, I hope you have found the answer to your question and you will not have to search about what is NULL Pointer In C and why and how they are used in C language.
However, if you want any information related to this post or related to programming language, or computer science, then comment below I will clear your all doubts
If you want a complete tutorial on C language, then see here C Language Tutorial. Here you will get all the topics of C Programming Tutorial step by step.
Friends, if you liked this post, then definitely share this post with your friends so that they can get information about Null Pointer In C
To get the information related to Programming Language, Coding, C, C ++, subscribe to our website newsletter. So that you will get information about our upcoming new posts soon.