ProgrammingModerate
Which argument is correct as the size parameter in qsort for an array struct P recs[] of n records?
- A. sizeof(struct P *)
- B. sizeof recs - 1
- C. sizeof(recs) / 2
- D. sizeof recs[0]
Correct answer: D. sizeof recs[0]
The size is the size of one element. sizeof recs gives the whole array, and a pointer's size is wrong for an array of structs.
More C Programming questions
- A comparator for qsort is written as int cmp(const void a, const void b) { return ((const struct P )a)->k > ((const struct P )b)->k; }…
- For a struct array sorted by an int key, which bsearch call is correct to find a record with key k?
- A bsearch on a struct array returns NULL for a key that is actually present. Which is the most likely cause?
- A struct {int a; char b;} is used in an array of 3 elements on a platform with 4-byte int and natural alignment. What is sizeof(arr)?
- Using natural alignment, what is sizeof(struct { char c; short s; })?
- For struct { char name[4]; int v; } with a 4-byte int and natural alignment, what is sizeof of the struct?