Different Types of Pointers in C

Introduction

If you are aiming at a career in programming, pointers are a must-know. Pointers allow programmers to refer to (and manipulate) data in computer memory that is not stored in the current location.

A pointer is an abstraction of a physical address in memory. A pointer variable holds the address or location of another variable.

C is among the most popular and widely used general-purpose programming language. It is probably the oldest programming language that is still in use. Moreover, C climbed to the number one position in the TIOBE index in 2021 suppressing every other major programming language such as Java, Python, and Java.

Created in 1972, C has held a prominent place in the programming community. It has influenced many major programming languages. Though modern programming languages have many advanced features, many of them come from C. One of these important features of the C language is pointers. In this article, we will focus on the type of pointers in C but first will discuss pointers briefly.

Pointers in C Programming

A pointer in the C programming language is defined as a variable that stores the address of another variable. By address, it means the direct memory location of the variable.

A pointer is declared just like any other variable in C. The only difference is that a pointer name has an asterisk (*) in front of it. Observe the following.

```
int *pt1;
float *pt2;
double *pt3;
```

Note: It is mandatory to assign an asterisk symbol.

Working with a pointer is simple. First, we define a pointer variable, then we assign a value to it, and finally access the value using an asterisk (*) symbol or address without the asterisk (*) symbol.

Type of pointers in C

C has eight types of pointers. Each varies in terms of value and behavior. Let’s discuss each of them one by one with the help of examples.

Null pointer

The null pointer is the simplest and one of the commonly used pointers in C. As the name of the pointer implies, the pointer having a Null value is known as a Null pointer.

Basically, a Null pointer has 0 as its value. Such pointers are useful when we do not want to assign any address to a variable. Observe the following C code.

```
#include <stdlib.h>
#include <stdio.h>

int main(){
   // creating a NULL pointer
   int *pt1 = NULL;  
}
```

Dangling pointer

Memory allocation is not permanent. Sooner or later it is deleted. So if the memory, to which a pointer is pointing, is deleted, then that pointer is called a dangling pointer.

```
#include <stdlib.h>
#include <stdio.h>
int main()
{
    int *pt1 = (int *)malloc(sizeof(int));
  
    free(pt1); // this will turn the pointer *pt1 into a dangling pointer
}
```

Void pointer

Pointers, usually, have a data type associated with them. But if we want to define a pointer without any data type, we can do it by creating a void pointer.

A void pointer, as the name suggests, is a pointer created using the void keyword.

```
#include <stdlib.h>
#include <stdio.h>
int main()
{
    // void pointer
    void *pt1 = NULL; 
}
```

Wild pointer

In C, it is also possible to create pointers without initializing. Such pointers are called wild pointers.

```
#include <stdlib.h>
#include <stdio.h>
int main()
{
    // wild pointer
    void *pt1; 
}

Note: If a wild pointer is given a value afterward, then it is no longer a wild pointer.

Complex pointers

A complex pointer is defined in a complex manner. Observe the following pointer.

```
int *(*p[]) () ); 
```

It is not like the pointers we discussed in this article. It is a complex pointer and to understand it, we need to decipher it properly. Let’s decipher it step by step.

  1. First, start from the innermost part. It can be parentheses that have an identifier.

(*p[]) – p is an identifier here and square brackets indicate it is an array. Moreover, it has an asterisk (*) symbol in front of it. This means it is a pointer. So, we can say “p is an array of pointers”.

Note: [] and () should be given more priority than *.

  1. Now, let’s find another () or [] near the innermost part.

(*p[]) () – Another () implies “p is an array of pointers to a function”.

  1. After () and [], look for another *.

*(*p[]) () ) – With another * in front of the parenthesis implies “p is an array of pointers to a function returning pointers”.

  1. When identifiers, (), [], and * are finished, look for the data type.

int *(*p[]) () ) – int implies “p is an array of pointers to a function returning pointers of int data type”.

Near, Far, and Huge pointers

Near pointer stores 16-bit addresses while far and huge pointers are capable of storing 32-bit addresses. While the near pointer can access memory only with a 16-bit segment of the machine, the other two can access outside the memory. The difference between far and huge pointers is that the segment can be modified in huge pointers but it is not possible in far pointers.

Summary

  • Pointers in C are used to store addresses of other variables.
  • A pointer is created using an asterisk symbol.
  • There are eight types of pointers in C.
  • A pointer with a NULL value is called a Null pointer.
  • A pointer pointing towards an address that is deleted is called a dangling pointer.
  • A pointer with a void data type is called a void pointer.
  • A pointer defined without initialization is called a wild pointer.
  • A pointer with a complex value is called a complex pointer.
  • Apart from these, there are three more pointers – near, far, and huge.

Recent Articles

Related Stories

Leave A Reply

Please enter your comment!
Please enter your name here

Subscribe to get IQ's , Tutorials & Courses