If your sort criteria is truly nadrze[a][0] + nadrze[a][1] - nadrze[b][0] - nadrze[b], I find the following example to represent it:
#include <stdio.h>
#include <stdlib.h>
#define max_i 5
#define max_j 5
int srovnejVelikost(const void *p1, const void *p2) {
const long int *a = p1;
const long int *b = p2;
return ((a[0] + a[1]) - (b[0] + b[1]));
}
int main(int argc, char **argv)
{
int i = 0;
int j = 0;
long int nadrze[max_i][max_j];
long int d = 0;
printf("Before\n");
for (i = 0; i < max_i; ++i) {
d = (nadrze[i][0] + nadrze[i][1]);
printf("i: %d %d\n",i, d);
}
qsort(nadrze, max_i, sizeof(long int)*max_j, srovnejVelikost);
printf("After\n");
for (i = 0; i < max_i; ++i) {
d = (nadrze[i][0] + nadrze[i][1]);
printf("i: %d %d\n",i, d);
}
return(EXIT_SUCCESS);
}
You can expand out max_i and max_j as your case needs. I just used defines as I was testing.
Also note, I coded it as (a[0] + a[1]) - (b[0] + b[1]) as this is equivalent to a[0] +a[1] - b[0] - b[1] and it makes more sense as I read it for a comparator.
You need to pass the whole row to qsort, which I do by saying the width is `sizeof(ling int)*max_j, which is 5 in your case.
Does this make sense?
(The qsort comparison function doesn't care if you pass it one int or an array, it is up to you on how you deal with it. So watch our you don't access beyond what your meant to.)
long inttointin your return value of your comparator. Just saying. Also, your logic in your comparator can easily cause overflow or underflow with proper(term used loosely) values. That said, try it with a smaller array (say 20 elements) and post the results in your question, along with the original array values and order.