EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Null pointer in C

Home » Software Development » Software Development Tutorials » C Programming Tutorial » Null pointer in C

Null pointer in C

Introduction to Null pointers in C

In C programming language, a variable that can point to or store the address of another variable is known as pointers. In C programming language pointers are used to point to the memory that is allocated dynamically or at run time and a pointer can be of any data type like int, float, char, etc. In this article, we are discussing the null pointer in C, where NULL is constant with value 0 in C. So the null pointer is defined as the pointer that is assigned to zero to make it null pointer or a pointer that does not store any valid memory address or an uninitialized pointer are known as a NULL pointer. In general, we can a pointer that does not point to any object is known as a null pointer.

How does Null pointer work in C?

A null pointer in C is a pointer that is assigned to zero or NULL where a variable that has no valid address. The null pointer usually does not point to anything. In C programming language NULL is a macro constant that is defined in a few of the header files like stdio.h, alloc.h, mem.h, stddef.h, stdlib.h. Also, note that NULL should be used only when we are dealing with pointers only. Simple syntax for declaring NULL pointer is as follows:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax #1

int *pointer_var = NULL;

Or

Syntax #2

We can directly assign the pointer variable to 0 to make it null pointer.

int *pointer_var = 0

Examples to Implement Null pointer in C

Let us see an example of how null pointers are created.

Example #1

Code:

#include <stdio.h>
int main ()
{
int  *ptr = NULL;
printf("The value of pointer assigned is : %x\n", ptr  );
return 0;
}

Output:

0 (zero)

Explanation: In the above code, we are initializing the variable “ptr”  to 0 (zero) so when we print the pointer value which Null pointer.

Popular Course in this category
C Programming Training (3 Courses, 5 Project)3 Online Courses | 5 Hands-on Projects | 34+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,570 ratings)
Course Price

View Course

Related Courses
C++ Training (4 Courses, 5 Projects, 4 Quizzes)Java Training (40 Courses, 29 Projects, 4 Quizzes)

Example #2

Suppose let us take another example where the pointer variables are not assigned to any memory address.

Code:

#include <stdio.h>
int main()
{
int *pointer_var;
printf("Address of the given pointer variable: %d", pointer_var);
printf("Value of pointer variable is : %d", * pointer_var);
return 0;
}

Output:

timeout

Explanation: In the above code, the pointer_var variable is not assigned to zero nor it stores any address of any variable in it, when the code is executed during the compile-time it gives an error where it throws garbage value which might harm your computer. So usually when we try to write or read from a null pointer we get run time error as we saw in the above code which we get segmentation fault which is a null pointer exception sometimes it also throws an exception as null pointer exception. In most of the examples, a null pointer is used to denote or indicate the end of the list.

Example #3

To avoid this exception we can rewrite the above code as

Code:

#include <stdio.h>
int main()
{
int * pointer_var =NULL;
if(pointer_var!=NULL)
{
printf("Value of pointer variable is : %d",* pointer_var);
}
else
{
printf("Invalid pointer");
}
return 0;
}

Output:

Null pointer in C - 3

Explanation: In the above-modified code, we assign a pointer_var to the “NULL” value and we check with the condition if the value of the pointer is null or not. In most of the operating system, codes or programs are not allowed to access any memory which has its address as 0 because the memory with address zero 0is only reserved by the operating system as it has special importance, which states that the pointer is not intended to point to any memory location that can be accessible. So by default, we can say that if a pointer is assigned to zero then it is nothing but it only points to nothing.

So there is a way to check for the pointer is null or not by using if(ptr) results in 1 if the pointer is not null and if(!ptr)  results in 1 when the pointer is null as we did in the above-modified program.

Example #4

Let us see the use of null pointers in C programming language as below:

Null pointers are used to avoid crashing down of the program: As we saw earlier if we declare any pointer without assigning anything to it then it takes garbage value where it may result in crashing of the system program. So to avoid such situations we use null pointers where variables are assigned or declared as NULL or zero which is known as a null pointer.

Code:

#include<stdio.h>
void func(int *ptrvarB)
{
if(ptrvarB == NULL)
{
//Handle NULL pointer input
printf("It is null pointer");
}
else
{
printf("It is not a null pointer");
}
}
void main()
{
int *ptrvarA = NULL;
func(ptrvarA);
}

Output:

Null pointer in C - 4

Explanation: In the above code, we are defining function func() where we are passing a pointer ptrvarA and when the function func() is called it checks if the passed pointer is a null pointer or not. So we have to check if the passed value of the pointer is null or not because if it is not assigned to any value it will take the garbage value and it will terminate your program which will lead to the crashing of the program.

  • Another use is when we are freeing up the memory locations: In a few cases we do not need the memory data anymore where we are again pointing to the same memory then we delete that data to free up the memory. But the pointer is still pointing to the same memory location even after deleting the data from that memory. Such pointers are called dangling pointers this also can be avoided by using null pointer by setting the dangling pointer to null.

Conclusion

In C programming language a Null pointer is a pointer which is a variable with the value assigned as zero or having an address pointing to nothing. So we use keyword NULL to assign a variable to be a null pointer in C it is predefined macro. And we should note that once the data is not in use the memory allocated to it must be freed else it will again lead to the dangling pointer. And also note that never declare any pointer without assigning NULL because the program when executes it throws an error during runtime.

Recommended Articles

This is a guide to Null pointer in C. Here we discuss how Null pointer work in C  with syntax and examples to implement with proper codes and outputs. You can also go through our other related articles to learn more –

  1. Pointers in C++
  2. Pointers in C#
  3. Pointers in C
  4. Pointers in Python

All in One Software Development Bundle (600+ Courses, 50+ projects)

600+ Online Courses

50+ projects

3000+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
C Programming Tutorial
  • Pointers
    • Pointers in C
    • Null pointer in C
    • Function Pointer in C
    • Double Pointer in C
    • Void Pointer in C
    • Const Pointer in C
    • Dangling Pointers in C
    • Pointer Arithmetic in C
  • Basic
    • Introduction to C
    • What is C
    • Career in C Programming
    • Advantages of C
    • How to Install C
    • Best C Compilers
    • Data Types in C
    • Variables in C
    • C Keywords
    • C Command
    • Command Line Arguments in C
    • C Literals
    • Constants in C
    • Unsigned Int in C
    • String in C
  • Operators
    • C Operators
    • Arithmetic Operators in C
    • Relational Operators in C
    • Assignment Operators in C
    • Logical Operators in C
    • Conditional Operator in C
    • Modulus Operator in C
    • Ternary Operator in C
    • Address Operator in C
    • Unary Operator in C
    • Operators Precedence in C
    • Left Shift Operator in C
  • Control Statement
    • Control Statements in C
    • If Statement in C
    • If-else Statement in C
    • Else if Statement in C
    • Nested if Statement in C
    • #else in C
    • Structure Padding in C
    • Nested Structure in C
    • Continue Statement in C
    • Break Statement in C
    • Switch Statement in C
    • Goto Statement in C
  • Loops
    • Loops in C
    • For Loop in C
    • While Loop in C
    • Do While Loop in C
    • Nested Loop in C
    • Infinite Loop in C 
  • Function
    • Math Functions in C
    • Hashing Function in C
    • Recursive Function in C
    • Power Function in C
    • fputs in C
    • C puts() Function
    • fprintf() in C
    • fseek() in C
    • Stderr in C
    • ASCII Value in C
    • strcat() in C
    • Inline Function in C
    • sizeof() in C
    • Function Prototype in C
    • C ftell()
  • Array
    • Arrays in C Programming
    • 2-D Arrays in C
    • 3D Arrays in C
    • Multidimensional Array in C
    • Array Functions in C
    • Strings Array in C
  • Sorting
    • Sorting in C
    • Heap Sort in C
  • Advanced
    • Constructor in C
    • Encapsulation in C
    • C Storage Classes
    • Static Keyword in C
    • File Handling in C
    • Queue in C
    • Hexadecimal in C 
    • typedef in C
    • Memory Allocation in C
    • Linked List in C
    • Volatile in C
    • Tokens in C
    • Expression in C
    • Regular Expression in C
    • Error Handling in C
    • Types of Errors in C
    • Preprocessor in C
    • Preprocessor Directives in C
    • fscanf() in C
    • #Pragma in C
    • #ifndef in C
    • #undef in C
    • Macros in C
  • C programs
    • Patterns in C Programming
    • Star Patterns in C
    • Number Patterns in C
    • Swapping in C
    • Reverse Number in C
    • Palindrome in C Program
    • Factorial in C
    • Fibonacci Series in C
    • Square Root in C
    • Random Number Generator in C
    • Prime Numbers in C
    • Escape Sequence in C
    • Reverse String in C
    • Leap Year Program in C
    • Anagram Program in C
    • Strong Number in C
    • String Concatenation in C
    • C Programming Matrix Multiplication
    • Decimal to Octal in C
    • Expression Evaluation in C
    • Decimal to Hexadecimal in C
  • Interview question
    • C Programming Interview Questions

Related Courses

C Programming Training Course

C++ Training Course

Java Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - C Programming Training Course Learn More