Skip to main content

17. Types of Pointers

 

Void Pointer

A void pointer is a pointer that has no data type associated with it. A void pointer can be easily typecasted to any pointer type i.e. it is a general purpose pointer variable.

It is not possible to dereference void pointers. Pointer arithmetic is not allowed in C standards with void pointers. This is because void pointer has no data type associated with it.

int x = 25;
char y = 'a';
void *p;
p = &x;  // void pointer stores address of int 'x'
p = &y;  // void pointer now holds address of char 'y'
*(   (int *)p   )    // = 25, Dereferencing a pointer
*(   (char *)p   )    // = a, Dereferencing a pointer
In dynamic memory allocation, malloc() and calloc() return void * type pointer. This allows these dynamic memory functions to be used to allocate memory of any data type. This is because these pointers can be typecasted to any pointer type.

NULL Pointer

A pointer that is not assigned any value but NULL (nothing) is known as the NULL pointer. If we don't have any address to be specified in the pointer at the time of declaration, we can assign NULL value.

int x = 25;
int *p = NULL;  // Null Pointer
p = &x;  // Null Pointer now holds address of int 'x'

Null pointer is a pointer which has a value reserved for indicating that the pointer or reference does not refer to a valid object. A null pointer is guaranteed to compare unequal to any pointer that points to a valid object. Dereferencing a null pointer is undefined behaviour in C, and a conforming implementation is allowed to measure that any pointer that is dereferenced is not null.

Null pointer is a specific pointer which is mentioned in C standards and it has specific purposes. Null pointer gives a functionality to C programmer to check whether a pointer is legitimate or not. It is used to initialize a pointer variable in cases where pointer variable has not been assigned any valid address yet. To pass a null pointer to a function argument when we don't want to pass any valid memory address. We can perform error handling while using pointers with C. Example of such error handling can be: dereferencing pointer variable only if it is not NULL.

NULL vs Uninitialized pointer : An uninitialized pointer contains an undefined value, whereas a null pointer stores a defined value.

NULL vs Void pointer : Null pointer is a value, whereas void pointer is a type.

Dangling Pointer

A pointer pointing to a freed memory location or the location whose content has been deleted is called a dangling pointer. Dangling pointers arise during object destruction when an object that has an incoming reference is deleted or deallocated, without modifying the value of the pointer, so that the pointer still points to the memory location of the deallocated memory.

Dangling pointers can introduce nasty bugs in our C programs. We can avoid issues caused by dangling pointers by initializing pointer to NULL. Assigning NULL value means pointer is a null pointer now.

Causes of Dangling Pointer
  • Deallocation of memory: When memory is deallocated, the pointer keeps pointing to the freed space.
          int *ptr = (int *) malloc(sizeof(int));;
          free(ptr);  // ptr now becomes a dangling pointer
          ptr = NULL;  // ptr no longer a dangling pointer
  • Returning local variables in function calls: 
          #include <stdio.h>
          int *func(){
               int a = 34;
// a is local variable and goes out of scope as the function return is over
               return &a;
          }
          int main(){
               int *ptr = func();  
// ptr points to invalid location and becomes a dangling pointer
               printf("%d", *ptr);
               return 0;
          }
  • Variable going out of scope:
          #include <stdio.h>
          int main(){
               int *ptr;
               {  // Scope starts
                    int i = 0;
                    ptr = &i;
               }  // Scope ends and ptr is now a dangling pointer
               printf("%d", *ptr);  
               return 0;
           }

Wild Pointer

Uninitialized pointers are known as wild pointers. These pointers point to some arbitrary location in memory and may cause a program to crash or behave badly. Dereferencing wild pointers can cause nasty bugs. It is suggested to always initialize unused pointers to NULL. If we just declare a pointer without defining it, and then dereferencing it will give us some garbage value. Such a pointer is called as wild pointer.

int a = 3;
int *ptr, *hd;  // ptr and hd are now wild pointers
ptr = &a;  // ptr is no longer a wild pointer
hd = NULL;  // hd is no longer a wild pointer

Comments