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

Java Program to check Krishnamurthy Number

In this tutorial, we will learn about Krishnamurthy numbers and write a Krishnamurthy Number program in Java. We will write two programs for Krishnamurthy number …
Read More

Java Program for ISBN Number

In this tutorial, we will learn about the ISBN (International Standard Book Number) and write a program to check for the ISBN Number in Java …
Read More

C++ Memory Management

Memory management refers to the process of managing the computer memory while assigning the space to the program’s variable to improve the overall performance. Requirement …
Read More

C++ Pointer to Pointer

As we know by now that a pointer stores the address of the pointed variable. But it is not the only use, pointer also stores …
Read More

C++ Passing Pointers to Functions

A function is a user-defined block of codes that executes some specific task assigned to it invoked by its name. If there is an argument …
Read More

C++ Null Pointer

We use a null pointer when we do not have the exact address to assign to a pointer. It is considered a good practice and …
Read More