The call to qsort must pass in a function pointer for a compare function to be u
ID: 3827649 • Letter: T
Question
The call to qsort must pass in a function pointer for a compare function to be used in sorting the array. The function header for qsort is void qsort (void* base, //array to be sorted size_t n, //length of array size_t size, //size of each entry int (*cmp) const void *p, const void *q)//function pointer for compare function Which of the following function can be passed to qsort in order to sort an integer array? a) int int_cmp (int *p, int *q) {return *p - *q} b) int int_cmp (const void *p, const void *q) {return * (int *) p - * (int *) q;} c) int int_cmp (const void *p, const void *q) {return (int *) p - (int *) q;} d) int int_cmp (int *p, int *q) {return * (int *) p - * (int *)q;}Explanation / Answer
Answer is B
Firstly we need to see whcih all function match requried function signature
Only b and c matches signature
Now in C we are chaning a void pointer to int pointer but are not derefercing it and subtraction is not allowed in pointer arithematic and it would result in error.
in b we are properly dereferncing the pointer and then returning the subtraction of those two values.
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.