ProgrammingHard

A function gets a pointer to a linked list and must keep the list unchanged but needs to walk it twice. Which kind of pointer parameter is right?

  1. A. struct N **head, because the caller must receive a copy
  2. B. struct N head, passing the node by value
  3. C. const struct N *head, so the walk cannot modify the nodes through the pointer
  4. D. void *head, with no type checking

Correct answer: C. const struct N *head, so the walk cannot modify the nodes through the pointer

A const pointer documents and enforces that the walk reads only. The other forms change the calling convention or lose the type.

More C Programming questions

Exams that ask this topic