int* ptr = NULL; //Is this going to avoid the problem
This will cause ptr to point to NULL which you can explicitly check for as a default/uninitialized value. It prevents the problem, but a careless programmer can still accidentally dereference a null pointer without checking, causing undefined behaviour.