ProgrammingVery hard

Records with a name and a score are stored in an array and sorted with a comparator that compares the names with strcmp. Which field access is correct inside the comparator if the arguments are const void *?

  1. A. ((const struct R *)a)->name
  2. B. ((const struct S *)a)->name
  3. C. ((const struct R *)a)->nam
  4. D. ((struct R)a)->name

Correct answer: A. ((const struct R *)a)->name

The argument is a void pointer, so it must be cast to a pointer to the struct first and then accessed with ->. The other forms are invalid or apply the cast to the wrong operand.

More C Programming questions

Exams that ask this topic