Wild Pointers in C: Definition, Risks, and Avoidance

Rumman Ansari   Software Engineer   2024-07-05 05:54:34   8622  Share
Subject Syllabus DetailsSubject Details 1 Questions
☰ TContent
☰Fullscreen

Table of Content:

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);
}