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:

Output:


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.

Pointers

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.

Output:


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.

Output:


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.

Output:

Example 3: Variable goes out of scope

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


MORE

Implement Insertion sort Program in C

In this tutorial, we will learn and write a C program to implement insertion sort. Before that you may go through the following topics in …
Read More

C Program for implementation of Selection sort

In this tutorial, we will learn and write a C program to implement selection sort using function. Before that you may go through the following …
Read More

C Program to Delete a Particular element from an Array

In this tutorial, we will write a C program to delete an element in an array by specifying the value. Before that, you should have …
Read More

C Program to sort a String in Ascending order

In this tutorial, we will write a c program to sort a string array in ascending order. Before that, you may go through the following …
Read More

C Program to find Maximum occurring Character in a String

In this tutorial, we will write a program in C to find the maximum occurring character in a string. Before that, you may go through …
Read More

Keith Number Program in C

In this tutorial, we will write a Keith number in C. It is one of the most asked questions in an interview. Before that, you …
Read More