Wild Pointers


Key points on wild pointers:
If any of the pointer is uninitialized, then that pointer is called wild pointer.
Wild pointers won't refer to any valid memory location.
Dereferencing wild pointer would cause segmentation fault.

Consider the following,
int func() {
      int *iptr;
      char *cptr;
}
Here, iptr and cptr are wild pointers.  Because, they are not initialized to NULL.

int func() {
     static int *int_ptr;
     char *char_ptr;
}
Here, int_ptr and char_ptr are not wild pointers.  Because, they are static variables.  By default, static variables are initialized to 0.

Below is the example C program on wild pointer.

  #include <stdio.h>
  int main() {
        int *ptr;  // wild pointer
        if (ptr) {
                printf("ptr is not NULL\n");
                *ptr = 10;
                printf("*ptr: %d\n", *ptr);
        }
        return 0;
  }

  Output: (Wild pointer example)
  jp@jp-VirtualBox:~/$ ./a.out
  ptr is not NULL
  Segmentation fault

Comments

Popular posts from this blog

Difference between asterisk and ampersand operators in c

kbhit in c