In C programming, which of the following is correct about why all pointers should be
In C programming, which of the following is correct about why all pointers should be
set to NULL if they are not pointing to anything?
AWhen a pointer is initialized to NULL, it does not help in avoiding dangling pointers
(pointers that reference invalid or deallocated memory).
BSetting pointers to NULL is merely a norm and has no effect on program execution.
CIf a pointer is not pointing to anything, the program won’t execute at all, giving a syntax
error message.
DWhen a pointer is initialized to NULL, it signifies that the pointer does not currently
reference any valid memory location.
Show answerHide answer
Correct answer: (D) When a pointer is initialized to NULL, it signifies that the pointer does not currently
reference any valid memory location.
Explanation
A NULL pointer explicitly marks that the pointer refers to no valid memory, so the code can test if (p == NULL) before use, avoiding accidental use of garbage or dangling addresses.