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 *?
- A. ((const struct R *)a)->name
- B. ((const struct S *)a)->name
- C. ((const struct R *)a)->nam
- 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
- Sorting 2 x 10^6 structs with qsort in C takes about how many comparisons?
- A list of records must be kept sorted while new records arrive one at a time. Which structure gives the best total cost for 10^5 inserts…
- Which sort is stable and runs in O(n log n) worst case, suitable for struct arrays that must keep equal-key order?
- How many records remain after a struct array of 7 records is filtered to drop those with a key equal to the previous key, when the array…
- Rotating the 2 x 2 matrix [[1, 2], [3, 4]] by 90 degrees counter-clockwise gives which first row?
- The matrix int a[3][3] = {[1][1] = 5}; is zero-initialised everywhere else (C99 designators). What is a[0][0] + a[1][1] + a[2][2]?