I recently wrote a program that sorts an array. For it, I needed to write a comparison function, which I will pass into it. My comparison function should have returned 1(if x > y), -1(if x < y) or 0(if x = y). I wrote a regular function (Function 1) using conditional expressions, but I was advised to write differently (Function 2). Is it better to write like that? Will a Boolean condition always return 1 for the truth?(I mean if x=0 and y=0 will we always havе (x==y)==1 ?)
Function 1:
int Icmp(void* x, void* y)
{
int a = *(int*)x;
int b = *(int*)y;
if (a > b)
return 1;
else if (a < b)
return -1;
else
return 0;
}
Function 2:
int Icmp(void* x, void* y)
{
return (*(int*)x > * (int*)y) - (*(int*)x < *(int*)y);
}