C Language



Admission Enquiry Form

  

Types of Pointer in C

Types of Pointer in C

    NULL Pointer
    Dangling Pointer
    Generic Pointers
    Wild Pointer
    Near Pointer
    Far Pointer
    Huge Pointers




  • Null Pointer

    • NULL Pointer is a pointer which is pointing to nothing.
    • The NULL pointer points the base address of the segment.
    • In case, if you don’t have an address to be assigned to a pointer then you can simply use NULL
    • The pointer which is initialized with the NULL value is considered as a NULL pointer.

    NULL is a macro constant defined in following header files –
    stdio.h
    alloc.h
    mem.h
    stddef.h
    stdlib.h

    Defining NULL Value
    #define NULL 0
    Below are some of the variable representations of a NULL pointer.

    float *ptr  = (float *)0;
    char  *ptr  = (char *)0;
    double *ptr = (double *)0;
    char *ptr   = '\0';
    int *ptr    = NULL;

    Example of NULL Pointer
    #include <stdio.h>
    void main()
    {
    int  *ptr = NULL;
    printf("The value of ptr is %u",ptr);
    }
    Output :
    The value of ptr is 0




  • Dangling Pointer in C

    • Dangling pointers arise when an object is deleted or de-allocated, without modifying the value of the pointer, so that the pointer still points to the memory location of the de-allocated memory.
    • In short, a pointer pointing to a non-existing memory location is called a dangling pointer.
    • A pointer pointing to a memory location that has been deleted (or freed) is called dangling pointer.

      Examples
      There are different ways where Pointer acts as dangling pointer.
      1 : Using free or de-allocating memory
      #include<stdlib.h>
      {
      char *ptr = malloc(Constant_Value);
      .......
      .......
      .......
      free (ptr);      /* ptr now becomes a dangling pointer */
      }
      We have declared the character pointer in the first step. After execution of some statements we have de-allocated memory which is allocated previously for the pointer.
      As soon as memory is de-allocated for pointer, pointer becomes dangling pointer.

       2 : Out of Scope
      #include<stdlib.h>
      void main()
      {
      char *ptr = NULL;
      .....
      .....
      {
      char ch;
      ptr = &ch;
      }
      .....   /* dp is now a dangling pointer */
      }

      Character Pointer is Declared in the first Step.
      Pointer Variable ‘ptr’ is pointing to Character Variable ‘ch’ declared in the inner block .
      As character variable is non-visible in Outer Block , then Pointer is Still Pointing to Same Invalid memory location in Outer block , then Pointer becomes “Dangling”

      Program:
      // Deallocating a memory pointed by ptr causes dangling pointer
      #include <stdlib.h>
      #include <stdio.h>
      int main()
      {
      int *ptr = (int *)malloc(sizeof(int));

      // After below free call, ptr becomes a dangling pointer
      free(ptr);

      // No more a dangling pointer
      ptr = NULL;
      }




  • Generic/Void Pointers in C

  • When a variable is declared as being a pointer to type void it is known as a generic pointer. Since you cannot have a variable of type void, the pointer will not point to any data. It is still a pointer though, to use it you just have to cast it to another kind of pointer first.
    Why Void Pointers is important

    • Suppose we have to declare integer pointer, character pointer and float pointer then we need to declare 3 pointer variables.
    • Instead of declaring different types of pointer variable it is feasible to declare single pointer variable which can act as an integer pointer, character pointer ,float pointer.

    Declaration of Void Pointer
    void * pointer_name;
    Example :
    void *ptr;    // ptr is declared as Void pointer
    char ch;
    int x;
    float y;

    ptr = &ch;  // ptr has address of character data
    ptr = &x;  // ptr has address of integer data
    ptr = &y;  // ptr has address of float data

    Explanation :
    void *ptr;

    • Void pointer declaration is shown above.
    • We have declared 3 variables of integer, character and float type.
    • When we assign the address of the integer to the void pointer, the pointer will become Integer Pointer.
    • When we assign the address of Character Data type to void pointer it will become Character Pointer.
    • Similarly, we can assign the address of any data type to the void pointer.
    • It is capable of storing the address of any data type

    Program:
    #include<stdio.h>
    void main()
    {
    int x;
    float y;
    char ch;
    void *ptr;
    x =9;
    ch= 'd';
    y=23.45;

      ptr = &x;
    printf("ptr points to the integer value %d\n", *(int*)ptr);

     ptr = &y;
    printf("ptr points to the float value %f\n", *(float*)ptr);

      ptr= &ch;
    printf("ptr now points to the character %c\n", *(char*) ptr);
    }




  • Wild Pointer

  • A Pointer in C that has not been initialized till its first use is known as the Wild pointer. A wild pointer points to some random memory location.

    Example
    void main()
    {
    int  *ptr;
    /* ptr is a wild pointer, as it is not initialized yet */
    printf("%d", *ptr);
    }

    How can we avoid Wild Pointers?
    We can initialize a pointer at the time of declaration by assigning the address of some object/variable or by NULL;

    #include<stdio.h>
    void main()
    {
    int num = 2;
    int  *ptr = &num; /* Initializing pointer */
    /* Ptr is not a wild pointer, it is pointing to the address of variable num */
    printf("%d", *ptr);
    }




  • Near Pointer

    • The pointer which can points only 64KB data segment or segment number 8 is known as near pointer.
    • That is near pointer cannot access beyond the data segment like graphics video memory, text video memory, etc. Size of near pointer is two byte. With the help of keyword near, we can make any pointer as near pointer.

    Example of Near Pointer
    #include<stdio.h>
    void main()
    {
    int x=25;
    int near* ptr;
    ptr=&x;
    printf(“%d”,sizeof(ptr));
    }

    Output: 2




  • Far Pointer

  • The pointer which can point or access whole the residence memory of RAM, i.e., which can access all 16 segments is known as far pointer.
    Size of the far pointer is 4 byte or 32 bit.
    Example of Far Pointer
    #include
    void main()
    {
    int x=10;
    int far *ptr;
    ptr=&x;
    printf("%d",sizeof (ptr));
    }

    Output : 4

  • Huge Pointer

  • The pointer which can point or access whole the residence memory of RAM i.e. which can access all 16 segments is known as a huge pointer.
    Size of the far pointer is 4 byte or 32 bit.
    Example of Huge Pointer
    #include
    int main()
    {
    char huge * far *p;
    printf("%d %d %d",sizeof(p),sizeof(*p),sizeof(**p));
    return 0;
    }

    Explanation:  p is the huge pointer, *p is the far pointer and **p is char type data variable.