Different types of pointers in c programming language.


1.Null Pointer.
A null pointer is a type of pointer in C that does not refer or point to any memory location.A null pointer is created by assigning a null value to the pointer. A null pointer can store the base address of the segment. A null pointer can be declared of any data type.
By default it has a value of 0 in it , that means the operating system reserves memory at address 0. The type of Null pointer is void and stores the Null value in it.
A null pointer has a reserved value which is defined in a stddef header file.
The Null pointer is not intended to point to an accessible memory location.
Dereferencing null pointer results in error, i.e., if we try to access as *nptr (where nptr is a NULL pointer), it will results in a null pointer exception.


Following program is the Illustration of Null pointer :
#include < stdio.h >
int main() {
 int *nptr = NULL;  //null pointer
 printf(“The value inside null pointer nptr is: %x”, nptr);
 return 0;
}
Output:
The value inside null pointer nptr is: 0


2.Wild pointer:
An uninitialized pointer in C is called a wild pointer. Wild pointers points to some arbitrary memory location. The wild pointer in C is declared but not initialized. That is the reason it points to any random memory location or may point to non-existent memory location. So it is very important to initialize wild pointer and avoid uninitialization.
The default value of wild pointer is garbage value.
Wild pointer may cause a program behave unexpectedly or crash.

C program code to Illustrate wild pointer:
#include <stdio.h>
int main()
{
 int *wldptr; //wild pointer
 printf("value of *wldptr is %d ", *wldptr);
 return 0;
}
Output:
The output of the above program is:
Runtime Error


3.Void Pointer:
The void pointer in C is generic pointer. Generic pointer means a pointer that is not associated with any data type. It is also called general purpose pointer.it can be type casted to any type, so different types of variables can be assigned to the void pointer.

C program for demonstration of void pointer:
#include<stdio.h>
int main() {
 void *vdptr = NULL;  //void pointer
 int a = 15;
 vdptr = &a;
 printf("value of *vdptr is %d ", *(int* ) vdptr);  //typecasted to int using (int *)
 return 0;
}
 Output:
 value of *vdptr is 15


4.Complex Pointer
A Complex pointers contains of [] , (),* , identifier, data type. The associativity and precedence of these operators is different.
Syntax:
char (* cmplx-ptr)[10];
cmplx-ptr is a pointer to a one-dimensional character array of size ten(10).


5. Dangling Pointer
A pointer referring to a memory location that has been deleted or uninitialized is known as dangling pointer.
C program to illustrate working of dangling pointer:

#include <stdlib.h>
#include <stdio.h>
int main() {
 //allocation of memory to to dng_ptr
 int *dng_ptr = (int* ) malloc(sizeof(int));
 // release the memory
 free(dng_ptr);
 // dng_ptr is pointing to a deleted memory location now.
 // now dng_ptr became dangling pointer
 printf("value of * dng_ptr is %d ", * dng_ptr);
 // Removing dangling pointer
 dng_ptr = NULL;
}
 Output:
 value of * dng_ptr is 0


6. Huge Pointer
A huge pointer is the pointer in c language that can point to any segment in the memory. this pointer is similar to a far pointer.
The size occupied by huge pointer is 4 bytes (32 bit), and can access up to 64k size in memory. Using huge pointer, the segment part can be changed.


7. Near pointer:
A near pointer in c programming is used to store 16-bit address.it works with data segments of memory that are in 64Kb of range. Near pointer is not able to access addresses outside of that data segment. The ‘near’ keyword is used to make any pointer as a near pointer.
C program to demonstrate use of near pointer.

#include<stdio.h>
int main() {
 int np = 42;
 int near * nptr = & np;
 int s = sizeof(nptr);
 printf("size of nptr is %d byte", s);
  return 0;
}
 Output:
 size of nptr is 2 byte


8.Far pointer:
A far pointer has the size of 4 bytes, and store the address in two 16-bit register that allow it to access the memory outside the current segment. The compiler allocates a segment register to store segment address and another register to store offset within the current segment.
C program for Illustration of far pointer:

#include<stdio.h>
int main()
 {
  int y = 24;
  int far * fptr = &y;
  int sz = sizeof(fptr);
   printf("size of far pointer fptr is %d byte", sz);
  return 0;
 }
 Output:
 size of far pointer fptr is 4 byte


Previous Topic:-->> Pointer to Structure in C. || Next topic:-->>Null Pointer in C.