QP School

Full Version: Uninitialized pointers in C
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Using pointers without initializing them or dereferencing NULL pointers can lead to crashes.

// Incorrect
int *ptr; // Uninitialized pointer
*ptr = 10; // Dereferencing NULL pointer

// Correct
int *ptr = NULL;
ptr = (int *)malloc(sizeof(int));
*ptr = 10;
free(ptr);