#include <stdio.h>
#define N 5
#define M 15
typedef struct {
int length;
char name[M];
} List;
int compList(const void* elem1, const void* elem2) {
return (((const List*)elem1)->length > ((const List*)elem2)->length) -
(((const List*)elem1)->length < ((const List*)elem2)->length);
}
/**
* @param arr the array
* @param num size of the array
* @param size size of each element of the array
* @param comp comparison function
*/
size_t findMax(void* arr, size_t num, size_t size, int (*comp)(const void *, const void*)) {
if (num <= 0) {
return -1;
}
else if (num == 1) {
return 0;
}
size_t index = 0;
void* max = arr;
void* curr = arr + size;
for (size_t i = 1; i < num - 1; ++i, curr += size) {
if (comp(curr, max) > 0) {
max = curr;
index = i;
}
//curr += size;
}
return index;
}
int main() {
List arr[N] = {
{17, "fghhh"},
{20, "fdsf"},
{5, "asds"},
{1, "armut"},
{6, "elma"},
};
//size_t %ld
printf("%ld", findMax(arr, N, sizeof(List), compList));
return 0;
}