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

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 …
Read More

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 …
Read More

Java Program to Find pair of Integers in Array whose sum is given Number

In this tutorial, we will write a program to find a pair of elements from an array whose sum equals a given number in java …
Read More

Program to Print Diamond Alphabet Patterns in C

In this tutorial, we will learn to write a C program to print Diamond patterns using alphabets/characters. However, in this tutorial, we will create a …
Read More

Half Diamond Pattern in C using Alphabets

In this tutorial, we will learn and code the half diamond alphabet patterns in C programming language. However, in this tutorial, we will create a …
Read More

Half Pyramid of Alphabets in C

In this tutorial, we will learn and code alphabet patterns in C programming language specifically the Half pyramid of alphabets in C programming. However, in …
Read More