473,387 Members | 1,859 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

qsorting type int**

Folks,

Attempting to q-sort an array of int pairs, e.g. {{0,1}, {0, 0}, ...}
allocated using malloc/calloc, but the arguments I'm passing to qsort
are producing the incorrect results (see listing 1). Works for auto
object duration (e.g. heap) as in listing 2. Any ideas?

Thanks,
BEA
/* listing 1 : using malloc/calloc */

#include <stdio.h>
#include <stdlib.h>

int compare( const void *arg1, const void *arg2 ) {
printf("arg1 %d, %d\n", ((int *) arg1)[0], ((int *) arg1)[1]);
printf("arg2 %d, %d\n", ((int *) arg2)[0], ((int *) arg2)[1]);
fflush(stdout);

if (((int *) arg1)[0] ((int *) arg2)[0]) {
return -1;
} else if (((int *) arg1)[0] == ((int *) arg2)[0]) {
if (((int *) arg1)[1] ((int *) arg2)[1]) {
return -1;
} else if (((int *) arg1)[0] == ((int *) arg2)[0]) {
return 0;
} else if (((int *) arg1)[0] < ((int *) arg2)[0]) {
return 1;
}
} else
return 1;
}

int main(int argc, char *argv[]) {
int i;
int **x;

x = malloc(6 * sizeof(int *));

for (i = 0; i < 6; ++i) {
x[i] = calloc(2, sizeof(int));
}

/* assignments */
x[0][0] = 1;
x[0][1] = 3;
x[1][0] = 0;
x[1][1] = 2;
x[2][0] = 2;
x[2][1] = 6;
x[3][0] = 0;
x[3][1] = 1;
x[4][0] = 1;
x[4][1] = 7;
x[5][0] = 2;
x[5][1] = 1;

for (i = 0; i < 6; ++i)
printf("%d, %d\n", x[i][0], x[i][1]);
printf("\n");
fflush(stdout);

/* Sort remaining args using Quicksort algorithm: */
qsort( (void *)x, 6, sizeof( x[0] ), compare );

printf("\n");
for (i = 0; i < 6; ++i)
printf("%d, %d\n", x[i][0], x[i][1]);

for (i = 0; i < 6; ++i) {
free(x[i]);
}
free(x);
}


=====

/* listing 2: using heap store */

#include <stdlib.h>

int compare( const void *arg1, const void *arg2 ) {
printf("arg1 %d, %d\n", ((int *) arg1)[0], ((int *) arg1)[1]);
printf("arg2 %d, %d\n", ((int *) arg2)[0], ((int *) arg2)[1]);

if (((int *) arg1)[0] ((int *) arg2)[0]) {
return -1;
} else if (((int *) arg1)[0] == ((int *) arg2)[0]) {
if (((int *) arg1)[1] ((int *) arg2)[1]) {
return -1;
} else if (((int *) arg1)[0] == ((int *) arg2)[0]) {
return 0;
} else if (((int *) arg1)[0] < ((int *) arg2)[0]) {
return 1;
}
} else
return 1;
}

int main(int argc, char *argv[]) {
int i;
int x[6][2] = {
{1,3},
{0,2},
{2,6},
{0,1},
{1,7},
{2,1}};

for (i = 0; i < 6; ++i)
printf("%d, %d\n", x[i][0], x[i][1]);
printf("\n");

/* Sort remaining args using Quicksort algorithm: */
qsort( (void *)x, 6, sizeof( x[0] ), compare );

printf("\n");
for (i = 0; i < 6; ++i)
printf("%d, %d\n", x[i][0], x[i][1]);
}

Mar 2 '07 #1
8 1824
changing to (int **) from int * in listing 1 worked...
thanks

On Mar 1, 5:38 pm, beagle...@hotmail.com wrote:
Folks,

Attempting to q-sort an array of int pairs, e.g. {{0,1}, {0, 0}, ...}
allocated using malloc/calloc, but the arguments I'm passing to qsort
are producing the incorrect results (see listing 1). Works for auto
object duration (e.g. heap) as in listing 2. Any ideas?

Thanks,
BEA

/* listing 1 : using malloc/calloc */

#include <stdio.h>
#include <stdlib.h>

int compare( const void *arg1, const void *arg2 ) {
printf("arg1 %d, %d\n", ((int *) arg1)[0], ((int *) arg1)[1]);
printf("arg2 %d, %d\n", ((int *) arg2)[0], ((int *) arg2)[1]);
fflush(stdout);

if (((int *) arg1)[0] ((int *) arg2)[0]) {
return -1;
} else if (((int *) arg1)[0] == ((int *) arg2)[0]) {
if (((int *) arg1)[1] ((int *) arg2)[1]) {
return -1;
} else if (((int *) arg1)[0] == ((int *) arg2)[0]) {
return 0;
} else if (((int *) arg1)[0] < ((int *) arg2)[0]) {
return 1;
}
} else
return 1;

}

int main(int argc, char *argv[]) {
int i;
int **x;

x = malloc(6 * sizeof(int *));

for (i = 0; i < 6; ++i) {
x[i] = calloc(2, sizeof(int));
}

/* assignments */
x[0][0] = 1;
x[0][1] = 3;
x[1][0] = 0;
x[1][1] = 2;
x[2][0] = 2;
x[2][1] = 6;
x[3][0] = 0;
x[3][1] = 1;
x[4][0] = 1;
x[4][1] = 7;
x[5][0] = 2;
x[5][1] = 1;

for (i = 0; i < 6; ++i)
printf("%d, %d\n", x[i][0], x[i][1]);
printf("\n");
fflush(stdout);

/* Sort remaining args using Quicksort algorithm: */
qsort( (void *)x, 6, sizeof( x[0] ), compare );

printf("\n");
for (i = 0; i < 6; ++i)
printf("%d, %d\n", x[i][0], x[i][1]);

for (i = 0; i < 6; ++i) {
free(x[i]);
}
free(x);

}

=====

/* listing 2: using heap store */

#include <stdlib.h>

int compare( const void *arg1, const void *arg2 ) {
printf("arg1 %d, %d\n", ((int *) arg1)[0], ((int *) arg1)[1]);
printf("arg2 %d, %d\n", ((int *) arg2)[0], ((int *) arg2)[1]);

if (((int *) arg1)[0] ((int *) arg2)[0]) {
return -1;
} else if (((int *) arg1)[0] == ((int *) arg2)[0]) {
if (((int *) arg1)[1] ((int *) arg2)[1]) {
return -1;
} else if (((int *) arg1)[0] == ((int *) arg2)[0]) {
return 0;
} else if (((int *) arg1)[0] < ((int *) arg2)[0]) {
return 1;
}
} else
return 1;

}

int main(int argc, char *argv[]) {
int i;
int x[6][2] = {
{1,3},
{0,2},
{2,6},
{0,1},
{1,7},
{2,1}};

for (i = 0; i < 6; ++i)
printf("%d, %d\n", x[i][0], x[i][1]);
printf("\n");

/* Sort remaining args using Quicksort algorithm: */
qsort( (void *)x, 6, sizeof( x[0] ), compare );

printf("\n");
for (i = 0; i < 6; ++i)
printf("%d, %d\n", x[i][0], x[i][1]);

}

Mar 2 '07 #2
be*******@hotmail.com writes:
int compare( const void *arg1, const void *arg2 ) {
printf("arg1 %d, %d\n", ((int *) arg1)[0], ((int *) arg1)[1]);
printf("arg2 %d, %d\n", ((int *) arg2)[0], ((int *) arg2)[1]);
fflush(stdout);

if (((int *) arg1)[0] ((int *) arg2)[0]) {
You're trying to treat these arguments as if they point to ints.
They don't. They point to int *s. Thus, you should be casting
them to int ** or, better yet, assigning them to a variable of
type "const int **".
return -1;
} else if (((int *) arg1)[0] == ((int *) arg2)[0]) {
[...]
/* Sort remaining args using Quicksort algorithm: */
qsort( (void *)x, 6, sizeof( x[0] ), compare );
qsort isn't necessarily implemented as a quicksort.

The cast here should be unnecessary.

[...]
/* listing 2: using heap store */
I don't see any use of malloc in this version. Usually that's
what programmers mean when they say "heap".
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Mar 2 '07 #3

On Thu, 1 Mar 2007 be*******@hotmail.com wrote:
>
Attempting to q-sort an array of int pairs, e.g. {{0,1}, {0, 0}, ...}
allocated using malloc/calloc, but the arguments I'm passing to qsort
are producing the incorrect results (see listing 1). Works for auto
object duration (e.g. heap) as in listing 2. Any ideas?
First guess, without even looking at the code: You'll be trying to
treat int[2] as if it were int*, or vice versa. Let's see if I'm right...
/* listing 1 : using malloc/calloc */

#include <stdio.h>
#include <stdlib.h>

int compare( const void *arg1, const void *arg2 ) {
printf("arg1 %d, %d\n", ((int *) arg1)[0], ((int *) arg1)[1]);
printf("arg2 %d, %d\n", ((int *) arg2)[0], ((int *) arg2)[1]);
fflush(stdout);

if (((int *) arg1)[0] ((int *) arg2)[0]) {
return -1;
} else if (((int *) arg1)[0] == ((int *) arg2)[0]) {
if (((int *) arg1)[1] ((int *) arg2)[1]) {
return -1;
} else if (((int *) arg1)[0] == ((int *) arg2)[0]) {
return 0;
} else if (((int *) arg1)[0] < ((int *) arg2)[0]) {
return 1;
}
} else
return 1;
}
Aaugh! Blecch! Ick! You shouldn't have any casts in your code, and
a straightforward comparison like this should be maybe five lines long,
at most. Here's something equivalent to what you wrote:

int compare( const void *va, const void *vb ) {
int *a = va;
int *b = vb;
printf("arg1 %d, %d\n", a[0], a[1]);
printf("arg2 %d, %d\n", b[0], b[1]);

if (a[0] b[0]) {
return -1;
} else if (a[0] == b[0]) {
return (a[1] b[1]) ? -1 : (a[0] < b[0]);
} else return 1;
}

Now, I notice two things: First, you have a couple of typos; you
write a[0] (or rather, ((int *)arg1)[0]) where you mean a[1] (or
rather, ((int *)arg1)[1]). So my code isn't really doing quite the
same thing as yours, because I'm not invoking undefined behavior
by falling off the end of the function without returning a value.

If you had compiled your code with warnings turned on ("gcc -W -Wall"),
you would have found this error at once.

The other thing I notice is that you're treating the input
arguments as if they were pointers to int, which is not the case ---
or if it is, then you're comparing different elements of the sorted
array, which isn't kosher at all. Let's see which is the case...
int main(int argc, char *argv[]) {
int i;
int **x;

x = malloc(6 * sizeof(int *));
x = malloc(6 * sizeof *x);
for (i = 0; i < 6; ++i) {
x[i] = calloc(2, sizeof(int));
x[i] = malloc(2 * sizeof *x[i]);
'calloc' is only useful if you need the memory zeroed for some reason.
Since you immediately assign new values into it, you don't need it to
be zeroed.
/* assignments */
[snip]
for (i = 0; i < 6; ++i)
printf("%d, %d\n", x[i][0], x[i][1]);
printf("\n");
fflush(stdout);
You don't need to 'fflush' an output stream if you've just written a
newline to it; the newline flushes the stream by itself already.
/* Sort remaining args using Quicksort algorithm: */
qsort( (void *)x, 6, sizeof( x[0] ), compare );
qsort(x, 6, sizeof *x, compare);

....And here's the problem, exactly as I thought at first. You're trying
to qsort an array of 'int*', but your comparison function is written
(almost) as if you're trying to sort an array of 'int[2]'.

The comparison function for 'qsort' should be written this way, in
order to sort an array of T:

int compare(const void *va, const void *vb)
{
const T *a = va;
const T *b = vb;
return (*a < *b) ? -1 : (*a *b);
}

Or if T is a small type, such as your int*:

int compare(const void *va, const void *vb)
{
T a = *(const T *)va;
T b = *(const T *)vb;
return (a < b) ? -1 : (a b);
}

Therefore, what you wanted was:

int compare(const void *va, const void *vb)
{
int *a = *(int * const *)va;
int *b = *(int * const *)vb;
if (a[0] < b[0]) return -1;
if (a[0] b[0]) return -1;
return (a[1] < b[1]) ? -1 : (a[1] b[1]);
}

Five lines --- what'd I tell you? :)

HTH,
-Arthur
Mar 2 '07 #4
On Mar 1, 6:02 pm, "Arthur J. O'Dwyer" <ajonos...@andrew.cmu.edu>
wrote:
On Thu, 1 Mar 2007 beagle...@hotmail.com wrote:
Attempting to q-sort an array of int pairs, e.g. {{0,1}, {0, 0}, ...}
allocated using malloc/calloc, but the arguments I'm passing to qsort
are producing the incorrect results (see listing 1). Works for auto
object duration (e.g. heap) as in listing 2. Any ideas?

First guess, without even looking at the code: You'll be trying to
treat int[2] as if it were int*, or vice versa. Let's see if I'm right...
/* listing 1 : using malloc/calloc */
#include <stdio.h>
#include <stdlib.h>
int compare( const void *arg1, const void *arg2 ) {
printf("arg1 %d, %d\n", ((int *) arg1)[0], ((int *) arg1)[1]);
printf("arg2 %d, %d\n", ((int *) arg2)[0], ((int *) arg2)[1]);
fflush(stdout);
if (((int *) arg1)[0] ((int *) arg2)[0]) {
return -1;
} else if (((int *) arg1)[0] == ((int *) arg2)[0]) {
if (((int *) arg1)[1] ((int *) arg2)[1]) {
return -1;
} else if (((int *) arg1)[0] == ((int *) arg2)[0]) {
return 0;
} else if (((int *) arg1)[0] < ((int *) arg2)[0]) {
return 1;
}
} else
return 1;
}

Aaugh! Blecch! Ick! You shouldn't have any casts in your code, and
a straightforward comparison like this should be maybe five lines long,
at most. Here's something equivalent to what you wrote:

int compare( const void *va, const void *vb ) {
int *a = va;
int *b = vb;
printf("arg1 %d, %d\n", a[0], a[1]);
printf("arg2 %d, %d\n", b[0], b[1]);

if (a[0] b[0]) {
return -1;
} else if (a[0] == b[0]) {
return (a[1] b[1]) ? -1 : (a[0] < b[0]);
} else return 1;
}

Now, I notice two things: First, you have a couple of typos; you
write a[0] (or rather, ((int *)arg1)[0]) where you mean a[1] (or
rather, ((int *)arg1)[1]). So my code isn't really doing quite the
same thing as yours, because I'm not invoking undefined behavior
by falling off the end of the function without returning a value.

If you had compiled your code with warnings turned on ("gcc -W -Wall"),
you would have found this error at once.

The other thing I notice is that you're treating the input
arguments as if they were pointers to int, which is not the case ---
or if it is, then you're comparing different elements of the sorted
array, which isn't kosher at all. Let's see which is the case...
int main(int argc, char *argv[]) {
int i;
int **x;
x = malloc(6 * sizeof(int *));

x = malloc(6 * sizeof *x);
for (i = 0; i < 6; ++i) {
x[i] = calloc(2, sizeof(int));

x[i] = malloc(2 * sizeof *x[i]);
'calloc' is only useful if you need the memory zeroed for some reason.
Since you immediately assign new values into it, you don't need it to
be zeroed.
/* assignments */

[snip]
for (i = 0; i < 6; ++i)
printf("%d, %d\n", x[i][0], x[i][1]);
printf("\n");
fflush(stdout);

You don't need to 'fflush' an output stream if you've just written a
newline to it; the newline flushes the stream by itself already.
/* Sort remaining args using Quicksort algorithm: */
qsort( (void *)x, 6, sizeof( x[0] ), compare );

qsort(x, 6, sizeof *x, compare);

...And here's the problem, exactly as I thought at first. You're trying
to qsort an array of 'int*', but your comparison function is written
(almost) as if you're trying to sort an array of 'int[2]'.

The comparison function for 'qsort' should be written this way, in
order to sort an array of T:

int compare(const void *va, const void *vb)
{
const T *a = va;
const T *b = vb;
return (*a < *b) ? -1 : (*a *b);

}

Or if T is a small type, such as your int*:

int compare(const void *va, const void *vb)
{
T a = *(const T *)va;
T b = *(const T *)vb;
return (a < b) ? -1 : (a b);

}

Therefore, what you wanted was:

int compare(const void *va, const void *vb)
{
int *a = *(int * const *)va;
int *b = *(int * const *)vb;
if (a[0] < b[0]) return -1;
if (a[0] b[0]) return -1;
return (a[1] < b[1]) ? -1 : (a[1] b[1]);

}

Five lines --- what'd I tell you? :)

HTH,
-Arthur

wow wow wow, thanks

Mar 2 '07 #5
"Arthur J. O'Dwyer" <aj*******@andrew.cmu.eduwrites:
int compare( const void *va, const void *vb ) {
int *a = va;
int *b = vb;
printf("arg1 %d, %d\n", a[0], a[1]);
printf("arg2 %d, %d\n", b[0], b[1]);

if (a[0] b[0]) {
return -1;
} else if (a[0] == b[0]) {
return (a[1] b[1]) ? -1 : (a[0] < b[0]);
} else return 1;
}
I always find this kind of thing most easily readable when I
write it like this:

if (a[0] != b[0])
return a[0] b[0] ? 1 : -1;
else if (a[1] != b[1])
return a[1] b[1] ? 1 : -1;
else
return 0;

although in this case I'd be tempted to use a for loop:

for (i = 0; i < 2; i++)
if (a[i] != b[i])
return a[i] b[i] ? 1 : -1;
return 0;
--
"...deficient support can be a virtue.
It keeps the amateurs off."
--Bjarne Stroustrup
Mar 2 '07 #6
"Arthur J. O'Dwyer" wrote:
>
.... snip ...
>
int compare(const void *va, const void *vb)
{
int *a = *(int * const *)va;
int *b = *(int * const *)vb;
if (a[0] < b[0]) return -1;
if (a[0] b[0]) return -1;
return (a[1] < b[1]) ? -1 : (a[1] b[1]);
}
How about:

int compare(const void *va, const void *vb) {
const int *a = va;
const int *b = vb;

if (*a == *b) {
a++; b++;
}
return (*a *b) - (*b *a);
}

No casts. Single exit.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

Mar 2 '07 #7
why use const int ** assignment better? i assume 'const' keyword helps
compile time optimizations?

as for heap, i cleared that confusion after reading this article
http://www.cs.ucla.edu/~kohler/z/mem...cationinc.html

funny at the time to read.

thanks

On Mar 1, 5:51 pm, Ben Pfaff <b...@cs.stanford.eduwrote:
beagle...@hotmail.com writes:
int compare( const void *arg1, const void *arg2 ) {
printf("arg1 %d, %d\n", ((int *) arg1)[0], ((int *) arg1)[1]);
printf("arg2 %d, %d\n", ((int *) arg2)[0], ((int *) arg2)[1]);
fflush(stdout);
if (((int *) arg1)[0] ((int *) arg2)[0]) {

You're trying to treat these arguments as if they point to ints.
They don't. They point to int *s. Thus, you should be casting
them to int ** or, better yet, assigning them to a variable of
type "const int **".
return -1;
} else if (((int *) arg1)[0] == ((int *) arg2)[0]) {

[...]
/* Sort remaining args using Quicksort algorithm: */
qsort( (void *)x, 6, sizeof( x[0] ), compare );

qsort isn't necessarily implemented as a quicksort.

The cast here should be unnecessary.

[...]
/* listing 2: using heap store */

I don't see any use of malloc in this version. Usually that's
what programmers mean when they say "heap".
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}

Mar 2 '07 #8
I used your template, need descending sort order, and finally this
worked. ("puts on cool looking sunglasses and goes outside")

Thanks

int compare(const void *va, const void *vb) {
int *a = *(int * const *)va;
int *b = *(int * const *)vb;

if (a[0] < b[0]) return 1;
if (a[0] b[0]) return -1;
return (a[1] < b[1]) ? 1 : (a[1] b[1]) ? -1 : 0;
}
On Mar 1, 6:02 pm, "Arthur J. O'Dwyer" <ajonos...@andrew.cmu.edu>
wrote:
On Thu, 1 Mar 2007 beagle...@hotmail.com wrote:
Attempting to q-sort an array of int pairs, e.g. {{0,1}, {0, 0}, ...}
allocated using malloc/calloc, but the arguments I'm passing to qsort
are producing the incorrect results (see listing 1). Works for auto
object duration (e.g. heap) as in listing 2. Any ideas?

First guess, without even looking at the code: You'll be trying to
treat int[2] as if it were int*, or vice versa. Let's see if I'm right...
/* listing 1 : using malloc/calloc */
#include <stdio.h>
#include <stdlib.h>
int compare( const void *arg1, const void *arg2 ) {
printf("arg1 %d, %d\n", ((int *) arg1)[0], ((int *) arg1)[1]);
printf("arg2 %d, %d\n", ((int *) arg2)[0], ((int *) arg2)[1]);
fflush(stdout);
if (((int *) arg1)[0] ((int *) arg2)[0]) {
return -1;
} else if (((int *) arg1)[0] == ((int *) arg2)[0]) {
if (((int *) arg1)[1] ((int *) arg2)[1]) {
return -1;
} else if (((int *) arg1)[0] == ((int *) arg2)[0]) {
return 0;
} else if (((int *) arg1)[0] < ((int *) arg2)[0]) {
return 1;
}
} else
return 1;
}

Aaugh! Blecch! Ick! You shouldn't have any casts in your code, and
a straightforward comparison like this should be maybe five lines long,
at most. Here's something equivalent to what you wrote:

int compare( const void *va, const void *vb ) {
int *a = va;
int *b = vb;
printf("arg1 %d, %d\n", a[0], a[1]);
printf("arg2 %d, %d\n", b[0], b[1]);

if (a[0] b[0]) {
return -1;
} else if (a[0] == b[0]) {
return (a[1] b[1]) ? -1 : (a[0] < b[0]);
} else return 1;
}

Now, I notice two things: First, you have a couple of typos; you
write a[0] (or rather, ((int *)arg1)[0]) where you mean a[1] (or
rather, ((int *)arg1)[1]). So my code isn't really doing quite the
same thing as yours, because I'm not invoking undefined behavior
by falling off the end of the function without returning a value.

If you had compiled your code with warnings turned on ("gcc -W -Wall"),
you would have found this error at once.

The other thing I notice is that you're treating the input
arguments as if they were pointers to int, which is not the case ---
or if it is, then you're comparing different elements of the sorted
array, which isn't kosher at all. Let's see which is the case...
int main(int argc, char *argv[]) {
int i;
int **x;
x = malloc(6 * sizeof(int *));

x = malloc(6 * sizeof *x);
for (i = 0; i < 6; ++i) {
x[i] = calloc(2, sizeof(int));

x[i] = malloc(2 * sizeof *x[i]);
'calloc' is only useful if you need the memory zeroed for some reason.
Since you immediately assign new values into it, you don't need it to
be zeroed.
/* assignments */

[snip]
for (i = 0; i < 6; ++i)
printf("%d, %d\n", x[i][0], x[i][1]);
printf("\n");
fflush(stdout);

You don't need to 'fflush' an output stream if you've just written a
newline to it; the newline flushes the stream by itself already.
/* Sort remaining args using Quicksort algorithm: */
qsort( (void *)x, 6, sizeof( x[0] ), compare );

qsort(x, 6, sizeof *x, compare);

...And here's the problem, exactly as I thought at first. You're trying
to qsort an array of 'int*', but your comparison function is written
(almost) as if you're trying to sort an array of 'int[2]'.

The comparison function for 'qsort' should be written this way, in
order to sort an array of T:

int compare(const void *va, const void *vb)
{
const T *a = va;
const T *b = vb;
return (*a < *b) ? -1 : (*a *b);

}

Or if T is a small type, such as your int*:

int compare(const void *va, const void *vb)
{
T a = *(const T *)va;
T b = *(const T *)vb;
return (a < b) ? -1 : (a b);

}

Therefore, what you wanted was:

int compare(const void *va, const void *vb)
{
int *a = *(int * const *)va;
int *b = *(int * const *)vb;
if (a[0] < b[0]) return -1;
if (a[0] b[0]) return -1;
return (a[1] < b[1]) ? -1 : (a[1] b[1]);

}

Five lines --- what'd I tell you? :)

HTH,
-Arthur

Mar 2 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Morten Gulbrandsen | last post by:
Dear MySQL developers, Could some experienced Database developer please take a look at this ? It is supposed to be plain SQL2. How can it be coded under MySQL Especially all referential...
10
by: Toke H?iland-J?rgensen | last post by:
Hello. I am quite new to the c++ language, and am still trying to learn it. I recently discovered how using include files would allow me to split up my code into smaller segments, instead of having...
11
by: Johan | last post by:
Hi Can somebody explain to me why I get this warning message and how I can solve this warning message. Thanks a lot Johan In member function `void
5
by: Axter | last post by:
I'm fine tuning a scope_handle class that takes a policy class as the second template. http://code.axter.com/scope_handle.h Please see above link for full understanding of the problem. One...
13
by: herrcho | last post by:
int intcmp(const void *a, const void *b) { return (*(int*)a - *(int*)b); } in the above , if i put just 'void' instead of 'const void' as a parameter, what's the difference ?
51
by: jacob navia | last post by:
I would like to add at the beginning of the C tutorial I am writing a short blurb about what "types" are. I came up with the following text. Please can you comment? Did I miss something? Is...
14
by: Matt | last post by:
I want to know if "int" is a primitive type, or an object? For example, the following two approaches yield the same result. > int t1 = int.Parse(TextBox2.Text); //method 1 > int t2 =...
2
by: Martin v. Löwis | last post by:
I've been working on PEP 353 for some time now. Please comment, in particular if you are using 64-bit systems. Regards, Martin PEP: 353 Title: Using ssize_t as the index type Version:...
10
by: kar1107 | last post by:
Hi all, Can the compiler chose the type of an enum to be signed or unsigned int? I thought it must be int; looks like it changes based on the assigned values. Below if I don't initialize...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.