C – Types of Pointers5 min read

In this tutorial we will learn about Null Pointer, Void Pointer, Wild Pointer and more. If you want to know more on Pointers, click the link given below.

There are eight different types of pointers, some of which we will learn here:

  1. Null pointer
  2. Void pointer
  3. Wild pointer
  4. Dangling pointer
  5. Complex pointer
  6. Near pointer
  7. Far pointer
  8. Huge pointer

NULL Pointer

When a pointer is assigned with a null value at the time declaration then it is called a NULL pointer. We use this value when we do not assign any address to the pointer. The null pointer always contains a constant value of 0.

C example to illustrate the use of NULL pointer:


Void Pointer

A void pointer is also called a generic pointer. This type of pointer can store the address of any variable no matter the data type. It is declared by the keyword void.

We have learned that the variable whose address is stored in the pointer must be of the same data type that of the pointer. For example: if we declare a pointer with int type then it cannot points to float type or any other type variable except int. So to avoid this problem void pointer is used. With the use of void, we can point the pointer to any variable with any of the data types without having to typecast.

But if you try to print the value of the variable to which the void pointer is pointing in the program directly then you will get a compile-time error: invalid use of void expression. So while printing the value, we need to typecast the void pointer to the required data type. We will illustrate it in a program below.

Syntax and declaration of void pointer:

C example to illustrate the use of Void pointer:

Output: We have discussed about the size of a pointer in Pointer Section.

C example of void pointer while printing values.

The following program tries to print the value of the variable to which the pointer pointing. Normally for any other data type, we simply print the value by *pointer_name. But here, it throws an error.

Output: Following compile-time error is shown.


Now to overcome this error and actually display the value of the variable, we will typecast the pointer while printing to remove the error as shown in the code below.


Wild Pointers

If a pointer is not initialized with a valid address or to anything then the pointer is called Wild Pointer. And because it is not initialized, it may be pointing to some unknown memory location which may cause a problem in the program.


Dangling Pointer

If a pointer points to the memory location that has been already deleted is called Dangling Pointer.

Dangling pointers mostly occurs when we are deleting or de-allocating an object (to which one of the pointers was pointing) without modifying the pointer value in the program. That means the pointer is still pointing to that deleted or de-allocated memory and those pointers are referred to as wild/dangling pointers. It causes the segmentation faults

There are three different situation where dangling pointer can occur. You need to use free() function to de-allocate the memory.

Example 1: De-allocation of memory

Example 2: Function call

The above program will through a segmentation fault. The problem will not appear if y is declared static. The dangling will not appear if the pointer is pointed to the static variable.
In the example below, we use the y as static.


Example 3: Variable goes out of scope

Here the pointer used is dangling pointer because of the scope. See the syntax below.


C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a number using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …

String Pattern Programs in C

In this tutorial, we will write various C pattern programs for String. Before that, you may go through the following topics in C. for loop …