| divine fallacy (34) | |||||||||||
|
qsort seems to accept as its last parameter: (1) a function; (2) the address of a function; and (3) a function pointer. Therefore, why is (3) in the declaration? Why not declare its last parameter as a regular function (therefore (2) and (3) become derivative alternatives)? DECLARATION OF QSORT
CONTEXT
(1)
(2)
(3)
| |||||||||||
|
|
|||||||||||
| Cubbi (1927) | |
| It takes a function pointer. 1, 2, and 3 are just different ways to construct a function pointer to call it with. | |
|
|
|
| vlad from moscow (3662) | |||||
These declarations
are equivalent. If a function is declared in one of the above ways the name of a function passed as argument is implicitly converted to a pointer to the function. So these calls
are also equivalent to the declarations shown above. | |||||
|
Last edited on
|
|||||
| Cubbi (1927) | |
| PS: great C interview question if the candidate wrote the three-way compare function this way: why is it wrong to compare values of type int using subtraction? (or, softer, "propose a unit test for this function") | |
|
|
|
| vlad from moscow (3662) | ||
I think you mean two-complement arithmetic with integers. Something as INT_MAX - INT_MIN | ||
|
|
||
| divine fallacy (34) | |
|
I'm not sure if this is what you were referring to (I'm obviously a novice), but is your question related to nasty behavior that results from subtracting unsigned / signed ints? I found this on Stack Overflow: "Edit: Actually, I could be wrong about the undefined behavior part. According to C language specification, the common part of the range of the corresponding signed and unsigned integer type shall have identical representation (implying, according to the footnote 31, "interchangeability as arguments to functions"). So, the result of a - b expression is unsigned 1001 as described above, and unless I'm missing something, it is legal to print this specific unsigned value with %d specifier, since it falls withing the positive range of int. Printing (unsigned) INT_MAX + 1 with %d would be undefined, but 1001u is fine." | |
|
|
|