Table of Contents

    Wild Pointers in C: Definition, Risks, and Avoidance

    Wild Pointers in C: Definition, Risks, and Avoidance

    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 of Wild Pointer

    int 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 point of declaration wither by the address of some object/variable or by NULL;

    int main() 
    {
      int val = 5;
      int  *ptr = &val; /* Initializing pointer */
      /* Ptr is not a wild pointer, it is pointing to the address of variable val */
      printf("%d", *ptr);
    }