Pointers in C/C++ (with examples) | Types of pointers



Previously, we learned about what is a pointer! How a pointer works and how to access the addresses and values manipulated by pointers. But Pointers are not limited only up to that. The wide applications of pointers have made them extend their abilities to a greater extent.

Depending on certain conditions, pointers can be classified into many types like a NULL pointer, void pointer, Wild pointer, Dangling pointer, complex pointer, Near pointer, far pointer, and Huge pointer. Each type has its own usage and characteristics. 

In this tutorial, we'll be mainly focussing upon the Null pointer, generic pointer/void pointer, and dangling pointers. And then we'll see how to use pointers with functions and doing arithmetics on pointers.

Null pointer in C

NULL Pointer is a special pointer value that is pointing to nothing. This means that NULL pointers do not actually point to a valid memory address. A predefined constant NULL is used to declare a NULL pointer, or we can assign value 0 to the pointer.

Example : int *ptr = NULL;  or   int *ptr = 0;

The NULL constant is defined in several header files including the <stdio.h> , <stdlib.h> and many more.

It is used in situations when a single pointer points to different locations at different times. So we can set the pointer to a NULL pointer when it does not point to any valid location. 

It is also used for performing Error-Handling operation in pointer code, like to check before dereferencing a pointer, whether it points to some valid address or is a NULL pointer.

if ( ptr == NULL )
{
     //Statement block
}

It can also be used to check whether a function in a program related to pointers is working correctly or not. As when a function does not operates correctly it returns a NULL pointer.

* If we try to dereference a NULL pointer, a logical error is thrown by the compiler.

Void Pointer in C

A void pointer is a pointer having void as its data type. It is a special pointer that is used to point to variables of any data type. It is also known as a generic pointer.
  
DECLARATION; void *ptr ;

Since we cannot declare a variable of void data type so therefore we also cannot dereference a generic pointer. We need to typecast the generic pointer to the necessary data type, before using it.

It is used in situations where we want to have a pointer that can point to different data type variables at different times.

#include<stdio.h>
int main()
{
int x=10;
char ch='C';
void *ptr;
ptr = &x ;
printf("This Generic pointer points to %d \n" , *(int*)ptr);
ptr = &ch ;
printf("Now this Generic pointer points to %d " , *(char*)ptr);
return 0;
}
OUTPUT : 

This Generic pointer points to 10     
Now this Generic pointer points to 67

Dangling pointer in C

It is a pointer that is pointing to a memory location that has been deleted or freed is known as a dangling pointer. It can be formed in the following ways
  • By freeing the memory that is being pointed to by the pointer
  • When the pointing address goes out of scope, the pointer becomes a dangling pointer. For example, in the main() function we declared a pointer variable 'p', inside it came an if-statement or for statement, where inside that 'p' points to some variable. and when it comes out of the if block, the address to which 'p' was pointing goes out of scope and 'p' becomes a dangling pointer.
Example:

Creating a dangling pointer by freeing the memory,

#include <stdlib.h> 
#include <stdio.h> 
int main() 
    int *d_ptr = (int *)malloc(sizeof(int)); 
    free(d_ptr);  
   /* Now the pointer d_ptr becomes a dangling pointer*/
}

Pointers and Functions!

In functions, where we send values as arguments (PASSING BY VALUES), we can also send the address of the variables and a pointer variable can be used to pick that address and perform operations on that address and the value stored at that address, this is called PASSING BY REFERENCE.

Example ; 

#include<stdio.h>
void swap(int *int *);
int main()
{
    int a,b;
    scanf("%d %d",&a,&b);
    swap(&a,&b);
    printf("After Swap a = %d and b = %d",a,b);
    return 0;
}
void swap( int *a,int *b)
{
    int temp;
    temp=*a;
    *a=*b;
    *b=temp;
}

Here we sending the address of the elements of the original variables which contain the values, now whatever change comes to them, it would be reflected in the main() also. Thatswhy in this example of swapping 2 numbers, the values would automatically be swapped in the main function variables.

Pointer Arithmetics!

Suppose, integer a is having the address location of 1000

int a=8;  
int *p=&a;
printf("%p",p); 

=> prints 1000 (address of a)

*p++; 
printf("%p",p); 

=> It increments the address one step forward and the pointer starts pointing to the adjacently next location to the memory address of a that is 1004

++*p; 
printf("%p",p); 

=> It increments the value at the pointing location by 1, that is, it would print 9

Hence, before using any arithmetic operations on pointers we should be very careful as any wrong impression can yield a wrong result which could be dangerous while building projects and big programs

Ritish

Just a novice blogger

Post a Comment (0)
Previous Post Next Post