CsTutorialPoint - Computer Science Tutorials For Beginners

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

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.

  • 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.

Here ptr is a NULL pointer.

Let’s understand NULL Pointer better through a program.

Example Program of Null pointer

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.

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.

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.

' src=

Jeetu Sahu is A Web Developer | Computer Engineer | Passionate about Coding, Competitive Programming, and Blogging

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Save my name, email, and website in this browser for the next time I comment.

Learn C++

12.8 — Null pointers

In the previous lesson ( 12.7 -- Introduction to pointers ), we covered the basics of pointers, which are objects that hold the address of another object. This address can be dereferenced using the dereference operator (*) to get the object at that address:

The above example prints:

In the prior lesson, we also noted that pointers do not need to point to anything. In this lesson, we’ll explore such pointers (and the various implications of pointing to nothing) further.

Null pointers

Besides a memory address, there is one additional value that a pointer can hold: a null value. A null value (often shortened to null ) is a special value that means something has no value. When a pointer is holding a null value, it means the pointer is not pointing at anything. Such a pointer is called a null pointer .

The easiest way to create a null pointer is to use value initialization:

Best practice

Value initialize your pointers (to be null pointers) if you are not initializing them with the address of a valid object.

Because we can use assignment to change what a pointer is pointing at, a pointer that is initially set to null can later be changed to point at a valid object:

The nullptr keyword

Much like the keywords true and false represent Boolean literal values, the nullptr keyword represents a null pointer literal. We can use nullptr to explicitly initialize or assign a pointer a null value.

In the above example, we use assignment to set the value of ptr2 to nullptr , making ptr2 a null pointer.

Use nullptr when you need a null pointer literal for initialization, assignment, or passing a null pointer to a function.

Dereferencing a null pointer results in undefined behavior

Much like dereferencing a dangling (or wild) pointer leads to undefined behavior, dereferencing a null pointer also leads to undefined behavior. In most cases, it will crash your application.

The following program illustrates this, and will probably crash or terminate your application abnormally when you run it (go ahead, try it, you won’t harm your machine):

Conceptually, this makes sense. Dereferencing a pointer means “go to the address the pointer is pointing at and access the value there”. A null pointer holds a null value, which semantically means the pointer is not pointing at anything. So what value would it access?

Accidentally dereferencing null and dangling pointers is one of the most common mistakes C++ programmers make, and is probably the most common reason that C++ programs crash in practice.

Whenever you are using pointers, you’ll need to be extra careful that your code isn’t dereferencing null or dangling pointers, as this will cause undefined behavior (probably an application crash).

Checking for null pointers

Much like we can use a conditional to test Boolean values for true or false , we can use a conditional to test whether a pointer has value nullptr or not:

The above program prints:

In lesson 4.9 -- Boolean values , we noted that integral values will implicitly convert into Boolean values: an integral value of 0 converts to Boolean value false , and any other integral value converts to Boolean value true .

Similarly, pointers will also implicitly convert to Boolean values: a null pointer converts to Boolean value false , and a non-null pointer converts to Boolean value true . This allows us to skip explicitly testing for nullptr and just use the implicit conversion to Boolean to test whether a pointer is a null pointer. The following program is equivalent to the prior one:

Conditionals can only be used to differentiate null pointers from non-null pointers. There is no convenient way to determine whether a non-null pointer is pointing to a valid object or dangling (pointing to an invalid object).

Use nullptr to avoid dangling pointers

Above, we mentioned that dereferencing a pointer that is either null or dangling will result in undefined behavior. Therefore, we need to ensure our code does not do either of these things.

We can easily avoid dereferencing a null pointer by using a conditional to ensure a pointer is non-null before trying to dereference it:

But what about dangling pointers? Because there is no way to detect whether a pointer is dangling, we need to avoid having any dangling pointers in our program in the first place. We do that by ensuring that any pointer that is not pointing at a valid object is set to nullptr .

That way, before dereferencing a pointer, we only need to test whether it is null -- if it is non-null, we assume the pointer is not dangling.

A pointer should either hold the address of a valid object, or be set to nullptr. That way we only need to test pointers for null, and can assume any non-null pointer is valid.

Unfortunately, avoiding dangling pointers isn’t always easy: when an object is destroyed, any pointers to that object will be left dangling. Such pointers are not nulled automatically! It is the programmer’s responsibility to ensure that all pointers to an object that has just been destroyed are properly set to nullptr .

When an object is destroyed, any pointers to the destroyed object will be left dangling (they will not be automatically set to nullptr ). It is your responsibility to detect these cases and ensure those pointers are subsequently set to nullptr .

Legacy null pointer literals: 0 and NULL

In older code, you may see two other literal values used instead of nullptr .

The first is the literal 0 . In the context of a pointer, the literal 0 is specially defined to mean a null value, and is the only time you can assign an integral literal to a pointer.

As an aside…

On modern architectures, the address 0 is typically used to represent a null pointer. However, this value is not guaranteed by the C++ standard, and some architectures use other values. The literal 0 , when used in the context of a null pointer, will be translated into whatever address the architecture uses to represent a null pointer.

Additionally, there is a preprocessor macro named NULL (defined in the <cstddef> header). This macro is inherited from C, where it is commonly used to indicate a null pointer.

Both 0 and NULL should be avoided in modern C++ (use nullptr instead). We discuss why in lesson 12.11 -- Pass by address (part 2) .

Favor references over pointers whenever possible

Pointers and references both give us the ability to access some other object indirectly.

Pointers have the additional abilities of being able to change what they are pointing at, and to be pointed at null. However, these pointer abilities are also inherently dangerous: A null pointer runs the risk of being dereferenced, and the ability to change what a pointer is pointing at can make creating dangling pointers easier:

Since references can’t be bound to null, we don’t have to worry about null references. And because references must be bound to a valid object upon creation and then can not be reseated, dangling references are harder to create.

Because they are safer, references should be favored over pointers, unless the additional capabilities provided by pointers are required.

Favor references over pointers unless the additional capabilities provided by pointers are needed.

Question #1

1a) Can we determine whether a pointer is a null pointer or not? If so, how?

Show Solution

Yes, we can use a conditional (if statement or conditional operator) on the pointer. A pointer will convert to Boolean false if it is a null pointer, and true otherwise.

1b) Can we determine whether a non-null pointer is valid or dangling? If so, how?

There is no easy way to determine this.

Question #2

For each subitem, answer whether the action described will result in behavior that is: predictable, undefined, or possibly undefined. If the answer is “possibly undefined”, clarify when.

2a) Assigning a new address to a non-const pointer

Predictable.

2b) Assigning nullptr to a pointer

2c) Dereferencing a pointer to a valid object

2d) Dereferencing a dangling pointer

2e) Dereferencing a null pointer

2f) Dereferencing a non-null pointer

Possibly undefined, if the pointer is dangling.

Question #3

Why should we set pointers that aren’t pointing to a valid object to ‘nullptr’?

We can not determine whether a non-null pointer is valid or dangling, and accessing a dangling pointer will result in undefined behavior. Therefore, we need to ensure that we do not have any dangling pointers in our program.

If we ensure all pointers are either pointing to valid objects or set to nullptr , then we can use a conditional to test for null to ensure we don’t dereference a null pointer, and assume all non-null pointers are pointing to valid objects.

guest

Dot Net Tutorials

Null Pointer in C

Back to: C Tutorials For Beginners and Professionals

Null Pointer in C Language with Examples

In this article, I will discuss Null Pointer in C Language with Examples. Please read our previous articles discussing Pointer to Constant in C Language with Examples.

What is a Null Pointer?

In C programming, a null pointer is a pointer that does not point to any valid memory location. It’s a special type of pointer used to indicate that it is not intended to point to an accessible memory location. Using a null pointer is essential for error handling and to avoid undefined behavior caused by uninitialized or dangling pointers. A null pointer is a special reserved value defined in a stddef header file. 

If we do not have any address which is to be assigned to the pointer, then it is known as a null pointer. When a NULL value is assigned to the pointer, it is considered a Null pointer. So, A null pointer is a pointer that points to nothing. Some uses of the null pointer are as follows:

  • Used to initialize a pointer variable when that pointer variable isn’t assigned any valid memory address yet.
  • Used to pass a null pointer to a function argument when we don’t want to pass any valid memory address.
  • Used to check for a null pointer before accessing any pointer variable. So that we can perform error handling in pointer-related code, e.g., dereference pointer variable only if it’s not NULL.

Characteristics of a Null Pointer in C Language:

  • Initialization: A null pointer is typically initialized using the macro NULL, defined in several standard libraries, like <stddef.h>, <stdio.h>, <stdlib.h>, and others.
  • Comparison: A null pointer can be compared against other pointers. It is often used in conditions to check whether a pointer is valid.
  • Assignment: Any pointer can be assigned NULL.
  • Dereferencing: Dereferencing a null pointer leads to undefined behavior, often resulting in a runtime error or a program crash.

Null Pointer in C Language:

The pointer variable, initialized with the null value, is called the Null Pointer. Null Pointer doesn’t point to any memory location until we are not assigning the address. The size of the Null pointer is also 2 bytes, according to the DOS Compiler.

Null Pointer in C Language with Examples

Example Usage of Null Pointers

Here’s a simple example demonstrating the use of null pointers:

When to Use Null Pointers in C Language?

  • Initialization: Initialize pointers to NULL when they are declared but not yet assigned to a specific memory address. This prevents them from becoming dangling pointers.
  • After free: Set pointers to NULL after freeing dynamically allocated memory to prevent dangling pointers.
  • Error Handling: Return NULL from functions to indicate an error when the function is supposed to return a pointer.
  • End of Data Structures: In linked lists, trees, and similar data structures, null pointers often signify the end of the structure or an empty node.
  • Conditional Checks: Check whether a pointer is NULL before dereferencing it to ensure that it points to valid memory.

Example: Initializing and Checking a Null Pointer

In this example, ptr is initialized to NULL and then checked. The program will print “The pointer is null.”

Example: Using Null Pointers in Function Return

A common use case for null pointers is in functions that return pointers. A null pointer can signal an error or a special condition.

Here, allocateMemory returns NULL if memory allocation fails. The main function checks the returned pointer and prints a message accordingly.

Example: Null Pointers in Linked Lists

Null pointers are frequently used in data structures like linked lists to mark the end of the list.

In this example, head is a null pointer indicating that the linked list is initially empty.

Key Points:

  • A null pointer does not point to any valid memory location.
  • It’s a good practice to initialize pointers to NULL until they are assigned a valid address.
  • Always check if a pointer is null before dereferencing it to avoid runtime errors.
  • Null pointers are a key part of many data structures and algorithms in C.

Null Pointer use Cases in C Language:

When we do not assign any memory address to the pointer variable..

In the below example, we declare the pointer variable *ptr, but it does not contain the address of any variable. The dereferencing of the uninitialized pointer variable will show the compile-time error as it does not point to any variable. The following C program shows some unpredictable results and causes the program to crash. Therefore, we can say that keeping an uninitialized pointer in a program can cause the program to crash.

How do we avoid the above problem?

We can avoid the problem in C Programming Language by using a Null pointer. A null pointer points to the 0th memory location, a reserved memory that cannot be dereferenced. In the below example, we create a pointer *ptr and assign a NULL value to the pointer, which means that it does not point to any variable. After creating a pointer variable, we add the condition in which we check whether the value of a pointer is null or not.

When we use the malloc() function?

In the below example, we use the built-in malloc() function to allocate the memory. If the malloc() function is unable to allocate the memory, then it returns a NULL pointer. Therefore, it is necessary to add the condition to check whether the value of a pointer is null. If the value of a pointer is not null, it means that the memory is allocated.

Note: It is always a good programming practice to assign a Null value to the pointer when we do not know the exact address of memory.

Applications of Null Pointer

Following are the applications of a Null pointer:

  • It is used to initialize the pointer variable when the pointer does not point to a valid memory address.
  • It is used to perform error handling with pointers before dereferencing the pointers.
  • It is passed as a function argument and returned from a function when we do not want to pass the actual memory address.

In the next article, I will discuss Void Pointer in C Language with Examples. In this article, I try to explain Null Pointers in C Language with Examples . I hope you enjoy this Null Pointer in C Language with Examples article. I would like to have your feedback. Please post your feedback, questions, or comments about this article.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

C++ Tutorial

  • C++ Overview
  • C++ Environment Setup
  • C++ Basic Syntax
  • C++ Comments
  • C++ Data Types
  • C++ Variable Types
  • C++ Variable Scope
  • C++ Constants/Literals
  • C++ Modifier Types
  • C++ Storage Classes
  • C++ Operators
  • C++ Loop Types
  • C++ Decision Making
  • C++ Functions
  • C++ Numbers
  • C++ Strings
  • C++ Pointers
  • C++ References
  • C++ Date & Time
  • C++ Basic Input/Output
  • C++ Data Structures
  • C++ Object Oriented
  • C++ Classes & Objects
  • C++ Inheritance
  • C++ Overloading
  • C++ Polymorphism
  • C++ Abstraction
  • C++ Encapsulation
  • C++ Interfaces
  • C++ Advanced
  • C++ Files and Streams
  • C++ Exception Handling
  • C++ Dynamic Memory
  • C++ Namespaces
  • C++ Templates
  • C++ Preprocessor
  • C++ Signal Handling
  • C++ Multithreading
  • C++ Web Programming
  • C++ Useful Resources
  • C++ Questions and Answers
  • C++ Quick Guide
  • C++ STL Tutorial
  • C++ Standard Library
  • C++ Discussion
  • Selected Reading
  • UPSC IAS Exams Notes
  • Developer's Best Practices
  • Questions and Answers
  • Effective Resume Writing
  • HR Interview Questions
  • Computer Glossary

C++ Null Pointers

It is always a good practice to assign the pointer NULL to a pointer variable in case you do not have exact address to be assigned. This is done at the time of variable declaration. A pointer that is assigned NULL is called a null pointer.

The NULL pointer is a constant with a value of zero defined in several standard libraries, including iostream. Consider the following program −

When the above code is compiled and executed, it produces the following result −

On most of the operating systems, programs are not permitted to access memory at address 0 because that memory is reserved by the operating system. However, the memory address 0 has special significance; it signals that the pointer is not intended to point to an accessible memory location. But by convention, if a pointer contains the null (zero) value, it is assumed to point to nothing.

To check for a null pointer you can use an if statement as follows −

Thus, if all unused pointers are given the null value and you avoid the use of a null pointer, you can avoid the accidental misuse of an uninitialized pointer. Many times, uninitialized variables hold some junk values and it becomes difficult to debug the program.

To Continue Learning Please Login

CSEstack

3 Major use of NULL Pointer in C Programming | What does actually NULL means?

Aniruddha chaudhari.

  • Updated: Mar 09, 2019

Understanding the NULL pointer is easy but not so if you are implementing in your code. At the end of this post, you will learn to avoid NULL pointer problems and handling them gracefully.

Following are the topics covered in this article.

Table of Contents

  • What is a NULL?

What is a NULL Pointer?

Why do we need a null pointer.

  • Best Practices to use NULL Pointer
  • What is the use of NULL Pointer in C?
  • Difference Between NULL and Void Pointer
  • Usage of a NULL Pointer in Various Programming Languages

So let’s begin…

What is NULL?

It is a special marker or keyword which has no value.

Each programming language has its own nuance.

In most of the programming language including C, Java, typically, 0 (zero) is a NULL and predefined constant or Macro.

NULL is a value that is not a value. Confusing, right?

Here is a simple example to differentiate.

In C++ programming,

char *par = 123; is not valid and gives a compilation error. Compiler except passing a hexadecimal value to the pointer and we are passing integer value.

Whereas  char *par = 0; is a valid statement. Here, 0 (zero) is a null in C++ programming. As null does not have any value, the compiler can not discriminate.

For this reason, C.A.R Hoare (inventor of Null) said that inventing the NULL pointer was his biggest mistake. We leave this here, as it is not the scope of this article.

Moving to…

Whenever you assign any data to the variable, it gets stored at a particular location in the physical memory. This memory location has unique address value in hexadecimal format (something like 0x006CoEEA8 ).

The variable that stores this memory address is called as a  Pointer .

When you assign a NULL value to the pointer, it does not point to any of the memory locations. The null pointer is nothing but the pointer which points to nothing.

It is also called as a NULL macro .

Here is a simple C program to print the value of NULL macro.

We can use this NULL constant value to assign to any pointer so that it will not point to any of the memory locations.

Here is the simple syntax for declaring a NULL pointer.

Here, ptr is a NULL pointer.

We can also assign 0 directly to the pointer.

This is also a valid expression in C. But, it is a standard practice to use a NULL constant.

The NULL constant is defined in many of the header files in the C programming language; including,  stdio.h   stddef.h , stdlib.h , etc.

In C programming, usually, we add stdio.h in the program to use scanf() and printf() functions. So, you don’t need to add any extra header files.

Later in the code, you can assign any memory location to ptr pointer.

Whenever you declare a pointer in your program, it points to some random memory location. And when you try to retrieve the information at that location, you get some garbage values. Many time, you might have observed this.

Using this garbage value in the program or passing it to any function, your program may crash.

Here, NULL pointer comes handy.

I am describing the use of the NULL pointer in C programming by three different ways. Before that, let’s see the best practices to use NULL pointer in programming.

Best Practices for NULL Pointer Usage:

How to use a NULL pointer to avoid any errors in your programming?

  • Make a habit of assigning the value to a pointer before using it. Don’t use pointer before initializing it.
  • If you don’t have a valid memory address to store in a pointer variable, just initialize a pointer to NULL.
  • Before using a pointer in any of your function code, check if it has not a NULL value.

What is the use of NULL Pointer in C?

Above all understanding, this is the first question you ask yourself about the NULL pointer. Here are some use cases of NULL pointer…

1. Avoid Crashing a Program:

If you pass any garbage value in your code or to the particular function, your program can crash. To avoid this, you can use NULL pointer.

Before using any pointer, compare it with NULL value and check.

In the above code, we are passing a pointer to fact() function. In fact() function, we are checking if the input pointer is NULL or not.

If the value of the pointer ptrA is not NULL, execute the function body.

Passing a NULL value to the function code without checking can terminate your program by crashing inside the function. So, it is one of the best use of NULL pointer in C.

2. While Freeing (de-allocating) Memory:

Suppose, you have a pointer which points to some memory location where data is stored. If you don’t need that data anymore, for sure, you want to delete that data and free the memory.

But even after freeing the data, pointer still points to the same memory location. This pointer is called as a dangling pointer . To avoid this dangling pointer, you can set the pointer to NULL.

Let’s check this below example to avoid dangling pointer in C.

Here, malloc() is an inbuilt function to create a dynamic memory.

What is the difference between NULL and Void Pointer?

Many of the programmer, especially beginners, get confused between NULL and void pointer.

The void is one of the data types in C. Whereas, NULL is the value which is assigned to the pointer.

The data type of the pointer is nothing but the type of data stored at the memory location where the pointer is pointed. When you are not sure about the type of data that is going to store at a particular memory location, you need to create the void pointer .

Below is an example for creating void pointer in C.

3. NULL pointer Uses in Linked List:

A NULL pointer is also useful in Linked List. We know that in Linked List, we point one node to its successor node using a pointer.

Implement Linked List in C

As there is no successor node to the last node, you need to assign a NULL value to the link of the last node. (As shown in above image.)

Check the implementation of Linked List in C to know how NULL pointer is used. I have described it in detail.

This is all about NULL pointer in C and CPP programming. The understanding of the NULL pointer is a concept. Like C programming, you can see the same use cases of NULL pointers in many other programming languages.

Usage of a NULL pointer in various Programming Languages?

Many of the programming languages use the NULL pointer concept. It is not necessary to have the same name for a NULL pointer, but the concept is almost the same in all the programming languages.

  • In C, the NULL keyword is a predefined macro.
  • In C++, the NULL is inherited from C programming.
  • The latest development in C++11, there is an explicit pointer to handle the NULL exception, called null ptr constant.
  • In Java programming , there is a null value. It indicates that no value is assigned to a reference variable.
  • In some other programming language like Lips, it is called as nil vector .

Check out all the C and C++ programming questions . You will find NULL pointer and macro very useful.

This is all about NULL macro and use of NULL pointer in C programming. If you have any question, feel free to ask in a comment.

Aniruddha Chaudhari

I am complete Python Nut, love Linux and vim as an editor. I hold a Master of Computer Science from NIT Trichy. I dabble in C/C++, Java too. I keep sharing my coding knowledge and my own experience on <b>CSEstack.org</b> portal.

Great Post. Agree with Mathew. Many people learn about NULL pointer but many few uses them in their project.

I think the NULL pointer is extremely useful to avoid the crash and for better programming.

You are absolutely right, Vatsal. Thanks for putting your thought.

I read about NULL pointer earlier, but this is very descriptive and you have mentioned very good use cases.

Thanks Aniruddha.

Great to see you here Mathew. I am glad you like it.

Very neatly explained. Thank you and keep up the good work. 🙂

Thanks Abha for putting your thought 🙂 It keeps motivating me to work hard.

output is 10. It still works. My program is not crashed.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Save my name, email, and website in this browser for the next time I comment.

C Programming

  • C- Introduction
  • C- Compile & Execute Program
  • C- Data Types
  • C- if-else statement
  • C- While, do-while, for loop
  • C- Function (Types/Call)
  • C- strlen() vs sizeof()
  • C- Nested Switch Statement
  • C- Recursion
  • C- Dynamic Programming
  • C- Storage Classes
  • C- Creating Header File
  • C- Null Pointer
  • C- Stack and Queue
  • C- Implement Stack using Array
  • C- Implement Linked List in C
  • C- File Handling
  • C- Makefile Tutorial

Programming for Practice

  • Online C/C++ Compiler

String Handling:

  • Remove White Spaces from String
  • Implement strstr Function in C
  • Convert String to Int – atoi()
  • Check if String is Palindrome
  • Check if Two Strings are Anagram
  • Split String in using strtok_r()
  • Undefined reference to strrev
  • Check if Array is Sorted

Bit Manipulation:

  • Count Number of 1’s in Binary

Linked List:

  • Reverse a Linked List Elements

Number System:

  • Program to Find 2’s Complement
  • Convert Decimal to Binary in C

Tricky Questions:

  • Add Two Numbers without Operator
  • Find Next Greater Number
  • Swap values without temp variable
  • Print 1 to 100 without Loop

Object Oriented Concepts in C++

  • C++: C vs OOPs Language
  • C++: Introduction to OOPs Concepts
  • C++: Inheritance

web analytics

Basic Programs

  • Hello World
  • Taking Input from User
  • Find ASCII Value of Character
  • Using gets() function
  • Switch Case
  • Checking for Vowel
  • Reversing Case of Character
  • Swapping Two Numbers
  • Largest and Smallest using Global Declaration
  • Basic for Loop
  • Basic while Loop
  • Basic do-while Loop
  • Nested for Loops
  • Program to find Factorial of number
  • Fibonacci Series Program
  • Palindrome Program
  • Program to find Sum of Digits
  • Program to reverse a String
  • Program to find Average of n Numbers
  • Armstrong Number
  • Checking input number for Odd or Even
  • Print Factors of a Number
  • Find sum of n Numbers
  • Print first n Prime Numbers
  • Find Largest among n Numbers
  • Exponential without pow() method
  • Find whether number is int or float
  • Print Multiplication Table of input Number
  • Reverse an Array
  • Insert Element to Array
  • Delete Element from Array
  • Largest and Smallest Element in Array
  • Sum of N Numbers using Arrays
  • Sort Array Elements
  • Remove Duplicate Elements
  • Sparse Matrix
  • Square Matrix
  • Determinant of 2x2 matrix
  • Normal and Trace of Square Matrix
  • Addition and Subtraction of Matrices
  • Matrix Mulitplication
  • Simple Program
  • Memory Management
  • Array of Pointers
  • Pointer Increment and Decrement
  • Pointer Comparison
  • Pointer to a Pointer
  • Concatenate Strings using Pointer
  • Reverse a String using Pointer
  • Pointer to a Function
  • Null Pointer
  • isgraph() and isprint()
  • Removing Whitespaces
  • gets() and strlen()
  • strlen() and sizeof()
  • Frequency of characters in string
  • Count Number of Vowels
  • Adding Two Numbers
  • Fibonacci Series
  • Sum of First N Numbers
  • Sum of Digits
  • Largest Array Element
  • Prime or Composite
  • LCM of Two Numbers
  • GCD of Two Numbers
  • Reverse a String

Files and Streams

  • List Files in Directory
  • Size of File
  • Write in File
  • Reverse Content of File
  • Copy File to Another File

Important Concepts

  • Largest of three numbers
  • Second largest among three numbers
  • Adding two numbers using pointers
  • Sum of first and last digit
  • Area and Circumference of Circle
  • Area of Triangle
  • Basic Arithmetic Operations
  • Conversion between Number System
  • Celsius to Fahrenheit
  • Simple Interest
  • Greatest Common Divisor(GCD)
  • Roots of Quadratic Roots
  • Identifying a Perfect Square
  • Calculate nPr and nCr

Miscellaneous

  • Windows Shutdown
  • Without Main Function
  • Menu Driven Program
  • Changing Text Background Color
  • Current Date and Time

Using Null Pointer Program

NULL is a macro in C, defined in the <stdio.h> header file, and it represent a null pointer constant. Conceptually, when a pointer has that Null value it is not pointing anywhere.

If you declare a pointer in C, and don't assign it a value, it will be assigned a garbage value by the C compiler, and that can lead to errors.

Void pointer is a specific pointer type. void * which is a pointer that points to some data location in storage, which doesn't have any specific type.

Don't confuse the void * pointer with a NULL pointer.

NULL pointer is a value whereas, Void pointer is a type.

Below is a program to define a NULL pointer.

Program Output:

C program example for Null Pointer

Use Null Pointer to mark end of Pointer Array in C

Now let's see a program in which we will use the NULL pointer in a practical usecase.

We will create an array with string values ( char * ), and we will keep the last value of the array as NULL. We will also define a search() function to search for name in the array.

Inside the search() function, while searching for a value in the array, we will use NULL pointer to identify the end of the array.

So let's see the code,

Peter is in the list. Scarlett not found.

This is a simple program to give you an idea of how you can use the NULL pointer. But there is so much more that you can do. You can ask the user to input the names for the array. And then the user can also search for names. So you just have to customize the program a little to make it support user input.

  • ← Prev
  • Next →

  C Tutorial

  c mcq tests.

  • Trending Now
  • Foundational Courses
  • Data Science
  • Practice Problem
  • Machine Learning
  • System Design
  • DevOps Tutorial

What is a Pointer to a Null pointer

  • Pointer to an Array | Array Pointer
  • C++ Pointer To Pointer (Double Pointer)
  • C - Pointer to Pointer (Double Pointer)
  • Passing NULL to printf in C
  • Dangling, Void , Null and Wild Pointers in C
  • Pointer to an Array in C++
  • How to Declare a Pointer to a Struct in C?
  • How to Declare a Pointer to a Union in C?
  • How to Delete a Pointer in C++?
  • How to Create a Pointer to a Function in C++?
  • What are Wild Pointers? How can we avoid?
  • NULL Pointer in C
  • NULL Pointer in C++
  • type_traits::is_null_pointer in C++
  • Pointer to Pointer in Objective-C
  • Go Pointer to Pointer (Double Pointer)
  • How to create a pointer to another pointer in a linked list?
  • What is a Pointing Device?
  • std::is_member_pointer in C++ with Examples

NULL pointer in C At the very high level, we can think of NULL as a null pointer which is used in C for various purposes. Some of the most common use cases for NULL are

  • To initialize a pointer variable when that pointer variable isn’t assigned any valid memory address yet. 
  • To check for a 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. 
  • To pass a null pointer to a function argument when we don’t want to pass any valid memory address. 

Pointer to a Null Pointer As Null pointer always points to null, one would think that Pointer to a Null Pointer is invalid and won’t be compiled by the compiler. But it is not the case. Consider the following example: 

Not only this program compiles but executes successfully to give the output as Output:

Explanation: What happens here is that when a Null pointer is created, it points to null, without any doubt. But the variable of Null pointer takes some memory. Hence when a pointer to a null pointer is created, it points to an actual memory space, which in turn points to null. Hence Pointer to a null pointer is not only valid but important concept.

Please Login to comment...

Similar reads.

  • Advanced Pointer
  • C-Advanced Pointer
  • C-Pointer Basics
  • cpp-double-pointer
  • cpp-pointer

Improve your Coding Skills with Practice

 alt=

What kind of Experience do you want to share?

Javatpoint Logo

  • Design Pattern
  • Interview Q

C Control Statements

C functions, c dynamic memory, c structure union, c file handling, c preprocessor, c command line, c programming test, c interview.

JavaTpoint

  • Send your Feedback to [email protected]

Help Others, Please Share

facebook

Learn Latest Tutorials

Splunk tutorial

Transact-SQL

Tumblr tutorial

Reinforcement Learning

R Programming tutorial

R Programming

RxJS tutorial

React Native

Python Design Patterns

Python Design Patterns

Python Pillow tutorial

Python Pillow

Python Turtle tutorial

Python Turtle

Keras tutorial

Preparation

Aptitude

Verbal Ability

Interview Questions

Interview Questions

Company Interview Questions

Company Questions

Trending Technologies

Artificial Intelligence

Artificial Intelligence

AWS Tutorial

Cloud Computing

Hadoop tutorial

Data Science

Angular 7 Tutorial

Machine Learning

DevOps Tutorial

B.Tech / MCA

DBMS tutorial

Data Structures

DAA tutorial

Operating System

Computer Network tutorial

Computer Network

Compiler Design tutorial

Compiler Design

Computer Organization and Architecture

Computer Organization

Discrete Mathematics Tutorial

Discrete Mathematics

Ethical Hacking

Ethical Hacking

Computer Graphics Tutorial

Computer Graphics

Software Engineering

Software Engineering

html tutorial

Web Technology

Cyber Security tutorial

Cyber Security

Automata Tutorial

C Programming

C++ tutorial

Control System

Data Mining Tutorial

Data Mining

Data Warehouse Tutorial

Data Warehouse

RSS Feed

IMAGES

  1. NULL Pointer in C example or What is NULL Pointer in C

    explain null pointer assignment

  2. NULL Pointer in C with example

    explain null pointer assignment

  3. Null pointer in C

    explain null pointer assignment

  4. PPT

    explain null pointer assignment

  5. NULL Pointer In C [Explained With Examples]

    explain null pointer assignment

  6. Null pointer assignment (Pointer part 6)

    explain null pointer assignment

VIDEO

  1. What are Pointers? How to Use Them? and How they can Improve your C++ Programming Skills

  2. Null Pointer Exception Song #2

  3. Wild Pointer & Null Pointer in C Language #cprogramming #ccode #computerlanguage

  4. Pointers in C

  5. What are Pointers? How to Use Them? and How they can Improve your C++ Programming Skills

  6. Null Pointer Exception #1

COMMENTS

  1. c

    A null pointer assignment error, or many other errors, can be assigned to this issue and example. In simpler architecture or programming environments, It can refer to any code which unintentionally ends up creating nulls as pointers, or creates a bug that in anyway halts the execution, like overwriting a byte in the return stack, overwriting ...

  2. NULL Pointer in C

    Syntax of Null Pointer Declaration in C type pointer_name = NULL; type pointer_name = 0;. We just have to assign the NULL value. Strictly speaking, NULL expands to an implementation-defined null pointer constant which is defined in many header files such as "stdio.h", "stddef.h", "stdlib.h" etc. Uses of NULL Pointer in C

  3. NULL Pointer In C [Explained With Examples]

    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 -: 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 -:

  4. 12.8

    A null value (often shortened to null) is a special value that means something has no value. When a pointer is holding a null value, it means the pointer is not pointing at anything. Such a pointer is called a null pointer. The easiest way to create a null pointer is to use value initialization:

  5. How to assign a value to a pointer that points to NULL

    1. A pointer can't point to null. It can be a null pointer, which means it doesn't point to anything. And a declared object can't be deleted as long as its name is visible; it only ceases to exist at the end of its scope. &value is a valid address at the time the assignment nullPointer = &value; is executed. It might become invalid later.

  6. NULL pointer in C

    NULL pointer in C - A null pointer is a pointer which points nothing.Some uses of the null pointer are:a) To initialize a pointer variable when that pointer variable isn't assigned any valid memory address yet.b) To pass a null pointer to a function argument when we don't want to pass any valid memory address.c) To.

  7. Null pointer in C

    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 ...

  8. Null Pointer in C Language with Examples

    In the below example, we create a pointer *ptr and assign a NULL value to the pointer, which means that it does not point to any variable. After creating a pointer variable, we add the condition in which we check whether the value of a pointer is null or not. #include <stdio.h>. int main()

  9. NULL Pointer in C Programming with Examples

    In exp. "char *cp = 0". cp is a NULL pointer! . In exp. "float *fp = 0". fp is a NULL pointer! We observed in above program, we saw how assigning ZERO '0' to pointer to any type, made it a NULL Pointer. This is a source code convention. Internally, however, the value for NULL pointer actually be something different and compiler takes care ...

  10. NULL Pointer in C++

    Syntax of Null Pointer in C++. We can create a NULL pointer of any type by simply assigning the value NULL to the pointer as shown: int* ptrName = NULL; // before C++11. int* ptrName = nullptr. int* ptrName = 0; // by assigning the value 0. A null pointer is represented by the value 0 or by using the keyword NULL.

  11. Dangling, Void , Null and Wild Pointers in C

    A null pointer stores a defined value, but one that is defined by the environment to not be a valid address for any member or object. NULL vs Void Pointer - Null pointer is a value, while void pointer is a type. Wild pointer in C. A pointer that has not been initialized to anything (not even NULL) is known as a wild pointer. The pointer may ...

  12. C++ Null Pointers

    C++ Null Pointers. It is always a good practice to assign the pointer NULL to a pointer variable in case you do not have exact address to be assigned. This is done at the time of variable declaration. A pointer that is assigned NULL is called a null pointer. The NULL pointer is a constant with a value of zero defined in several standard ...

  13. Learn What A Null Pointer Constant (nullptr) Is In Modern C++

    The nullptr keyword is a null pointer constant which is a prvalue of type std::nullptr_t. that denotes the pointer literal. C++11 introduced nullptr, the null pointer constant, to remove the ambiguity between 0 and a null pointer. Although the old style NULL macro exists, it is insufficient because it cannot be distinguished from the integer 0 ...

  14. 3 Major use of NULL Pointer in C Programming

    The NULL constant is defined in many of the header files in the C programming language; including, stdio.h stddef.h, stdlib.h , etc. In C programming, usually, we add stdio.h in the program to use scanf() and printf() functions. So, you don't need to add any extra header files. Later in the code, you can assign any memory location to ptr pointer.

  15. Using Null Pointer in Programs in C

    Using Null Pointer Program. NULL is a macro in C, defined in the <stdio.h> header file, and it represent a null pointer constant. Conceptually, when a pointer has that Null value it is not pointing anywhere. If you declare a pointer in C, and don't assign it a value, it will be assigned a garbage value by the C compiler, and that can lead to ...

  16. What is a Pointer to a Null pointer

    Pointer to a null pointer is valid. Explanation: What happens here is that when a Null pointer is created, it points to null, without any doubt. But the variable of Null pointer takes some memory. Hence when a pointer to a null pointer is created, it points to an actual memory space, which in turn points to null.

  17. Difference between Null Pointer and Dangling Pointer

    Null pointers and dangling pointers are two common errors that occur when we are dealing with pointers in programs of languages like C and C++. ... In the above example we have created a null pointer ptr and then try to assign the value 10 to the memory location pointed by it. But ptr is a null pointer so it does not point to any valid memory ...

  18. Null Pointer in C

    What is a Null Pointer? A Null Pointer is a pointer that does not point to any memory location. It stores the base address of the segment. The null pointer basically stores the Null value while void is the type of the pointer. A null pointer is a special reserved value which is defined in a stddef header file.

  19. c++

    The null pointer is a pointer with a value of 0. It doesn't strictly point to nothing, it points to absolute address 0, which generally isn't accessible to your program; dereferencing it causes a fault. It's generally used as a flag value, so that you can, for example, use it to end a loop. Update:

  20. c

    The problem is here: memset(&handler,0,sizeof(handler)); I assume you want to zero the struct, but handler is a pointer, so when you say &handler you're taking a pointer to the pointer and zeroing it. So effectively you're doing. handler = NULL;

  21. C++ pointer assignment

    Now we have two variables x and y: int *p = &x; int *q = &y; There are declared another two variables, pointer p which points to variable x and contains its address and pointer q which points to variable y and contains its address: x = 35; y = 46; Here you assign values to the variables, this is clear: p = q;