Question discussion

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?

358870Medium DSSSB TGT Computer Science 0 replies 0 views
0
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.
Correct answer: D

Explanation

Setting an unused pointer to NULL shows that it does not point to any valid memory location and helps avoid accidental use of garbage addresses.

Discussion 0

Nobody has replied yet. Start the discussion!