473,326 Members | 2,061 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,326 software developers and data experts.

Passing function pointer as argument to a function???

The library function 'qsort' is declared thus:
void qsort(void *base, size_t nmemb, size_t size,
int(*compar)(const void *, const void *));

If in my code I write:
int cmp_fcn(...);
int (*fcmp)() = &cmp_fcn;
qsort(..., fcmp);

then everything works. But if instead I code qsort as:

qsort(..., &cmp_fcn);

the compiler complains about incompatible pointer type.

Can someone help me understand exactly what I'm passing to
the qsort function in the "bad" code other than the pointer
to a function and/or how this differs from the "good" code?

How could the qsort statement be written directly without
the use of an intermediary like 'fcmp' ?

Thanks for your help.

Regards,
Charles Sullivan



Nov 15 '05 #1
17 3548


Charles Sullivan wrote On 09/19/05 11:05,:
The library function 'qsort' is declared thus:
void qsort(void *base, size_t nmemb, size_t size,
int(*compar)(const void *, const void *));

If in my code I write:
int cmp_fcn(...);
int (*fcmp)() = &cmp_fcn;
FYI: The `&' is harmless but unnecessary, both here
and below.
qsort(..., fcmp);

then everything works. But if instead I code qsort as:

qsort(..., &cmp_fcn);

the compiler complains about incompatible pointer type.

Can someone help me understand exactly what I'm passing to
the qsort function in the "bad" code other than the pointer
to a function and/or how this differs from the "good" code?
Well, let's see: The compiler says that cmp_fcn() is
of the wrong type. Might this mean that there's something
wrong with cmp_fcn()? It seems a possibility, doesn't it?
However, this is all just speculation on my part, since you
haven't (hint, hint) shown what cmp_fcn() looks like ...
How could the qsort statement be written directly without
the use of an intermediary like 'fcmp' ?


By writing a cmp_fcn() that matches what qsort()
requires.

--
Er*********@sun.com

Nov 15 '05 #2
Charles Sullivan wrote:

The library function 'qsort' is declared thus:
void qsort(void *base, size_t nmemb, size_t size,
int(*compar)(const void *, const void *));

If in my code I write:
int cmp_fcn(...);
int (*fcmp)() = &cmp_fcn;
qsort(..., fcmp);

then everything works. But if instead I code qsort as:

qsort(..., &cmp_fcn);

the compiler complains about incompatible pointer type.
qsort is expecting a pointer to a function which returns an int and
gets passed two const void * parameters. However, you are passing
it a pointer to a function which returns int and gets passed a
variable number of unknown parameters.
Can someone help me understand exactly what I'm passing to
the qsort function in the "bad" code other than the pointer
to a function and/or how this differs from the "good" code?
When using the intermediate fcmp variable, you are passing it
a function which returns int, and whose parameters are left
unprototyped.
How could the qsort statement be written directly without
the use of an intermediary like 'fcmp' ?


If cmp_fcn() does, in fact, take two const void * parameters, then
say so by changing:

int cmp_fcn(...);
to
int cmp_fcn(const void *,const void *);

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>

Nov 15 '05 #3
Charles Sullivan wrote:
The library function 'qsort' is declared thus:
void qsort(void *base, size_t nmemb, size_t size,
int(*compar)(const void *, const void *));

If in my code I write:
int cmp_fcn(...);
int (*fcmp)() = &cmp_fcn;
qsort(..., fcmp);

then everything works. But if instead I code qsort as:

qsort(..., &cmp_fcn);

the compiler complains about incompatible pointer type.

Can someone help me understand exactly what I'm passing to
the qsort function in the "bad" code other than the pointer
to a function and/or how this differs from the "good" code?

How could the qsort statement be written directly without
the use of an intermediary like 'fcmp' ?


Exactly as you use fcmp. To clarify:

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

#define ASIZE 10

int cmp_fcn(const void *e1, const void *e2);

int main(void)
{
double asrc[ASIZE], awrk[ASIZE];
int (*fcmp) () = cmp_fcn;
size_t i;
srand(time(0));
printf(" example using cmp_fcn as argument\n");
for (i = 0; i < ASIZE; i++)
asrc[i] = rand() / (1. + RAND_MAX);
memcpy(awrk, asrc, sizeof asrc);
qsort(awrk, ASIZE, sizeof *awrk, cmp_fcn);
for (i = 0; i < ASIZE; i++)
printf("%lu: %f %f\n", (unsigned long) i, asrc[i], awrk[i]);
printf("\n");

printf(" example using fcmp as argument\n");
for (i = 0; i < ASIZE; i++)
asrc[i] = rand() / (1. + RAND_MAX);
memcpy(awrk, asrc, sizeof asrc);
qsort(awrk, ASIZE, sizeof *awrk, fcmp);
for (i = 0; i < ASIZE; i++)
printf("%lu: %f %f\n", (unsigned long) i, asrc[i], awrk[i]);
return 0;
}
int cmp_fcn(const void *e1, const void *e2)
{
const double *p1 = e1, *p2 = e2;
return (*p1 > *p2) ? 1 : (*p1 < *p2) ? -1 : 0;
}

example using cmp_fcn as argument
0: 0.563498 0.111813
1: 0.195851 0.195851
2: 0.111813 0.254336
3: 0.294201 0.294201
4: 0.318893 0.318893
5: 0.586812 0.511567
6: 0.724280 0.563498
7: 0.925669 0.586812
8: 0.511567 0.724280
9: 0.254336 0.925669

example using fcmp as argument
0: 0.887919 0.053615
1: 0.053615 0.134536
2: 0.471209 0.211441
3: 0.457827 0.240006
4: 0.640444 0.457827
5: 0.211441 0.471209
6: 0.240006 0.511690
7: 0.511690 0.640444
8: 0.699548 0.699548
9: 0.134536 0.887919
Nov 15 '05 #4
Kenneth Brody <ke******@spamcop.net> writes:
Charles Sullivan wrote:

The library function 'qsort' is declared thus:
void qsort(void *base, size_t nmemb, size_t size,
int(*compar)(const void *, const void *));

If in my code I write:
int cmp_fcn(...);
int (*fcmp)() = &cmp_fcn;
qsort(..., fcmp);

then everything works. But if instead I code qsort as:

qsort(..., &cmp_fcn);

the compiler complains about incompatible pointer type.


qsort is expecting a pointer to a function which returns an int and
gets passed two const void * parameters. However, you are passing
it a pointer to a function which returns int and gets passed a
variable number of unknown parameters.


No, he's not. A function that takes a variable number of arguments
must have at least one named argument before the "...". Note also his
use of "..." in the call to qsort. The ellipsis isn't being used as C
syntax; it's just an ellipsis.

He's just not showing us exactly what he's doing -- which means we
can't guess what the problem is.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #5
Charles Sullivan wrote on 19/09/05 :
The library function 'qsort' is declared thus:
void qsort(void *base, size_t nmemb, size_t size,
int(*compar)(const void *, const void *));

If in my code I write:
int cmp_fcn(...);
int (*fcmp)() = &cmp_fcn;
You don't need the &.
qsort(..., fcmp);

then everything works. But if instead I code qsort as:

qsort(..., &cmp_fcn);
You don't need the &.

qsort(..., cmp_fcn);
the compiler complains about incompatible pointer type.
How exactly is cmp_fcn() prototyped ?
Can someone help me understand exactly what I'm passing to
the qsort function in the "bad" code other than the pointer
to a function and/or how this differs from the "good" code?
The compare function must exactly have this prototype :

int compare_function (void const *, void const *);
How could the qsort statement be written directly without
the use of an intermediary like 'fcmp' ?


Using the correct prototype for the compare function. We all do that
every day... (well, sort of)

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

I once asked an expert COBOL programmer, how to
declare local variables in COBOL, the reply was:
"what is a local variable?"
Nov 15 '05 #6
On Mon, 19 Sep 2005 20:12:17 +0000, Keith Thompson wrote:
Kenneth Brody <ke******@spamcop.net> writes:
Charles Sullivan wrote:

The library function 'qsort' is declared thus:
void qsort(void *base, size_t nmemb, size_t size,
int(*compar)(const void *, const void *));

If in my code I write:
int cmp_fcn(...);
int (*fcmp)() = &cmp_fcn;
qsort(..., fcmp);

then everything works. But if instead I code qsort as:

qsort(..., &cmp_fcn);

the compiler complains about incompatible pointer type.


qsort is expecting a pointer to a function which returns an int and
gets passed two const void * parameters. However, you are passing
it a pointer to a function which returns int and gets passed a
variable number of unknown parameters.


No, he's not. A function that takes a variable number of arguments
must have at least one named argument before the "...". Note also his
use of "..." in the call to qsort. The ellipsis isn't being used as C
syntax; it's just an ellipsis.

He's just not showing us exactly what he's doing -- which means we
can't guess what the problem is.


Mea culpa. I had forgotten about the C syntax and used "..." to
represent stuff I _thought_ irrelevant to the question.

Here's an example which illustrates the way I've been using
qsort:
-------------------------------------
#include <stdio.h>
#include <stdlib.h>

int val[] = { 3, 2, 1, 4, 5 };

int cmp_fcn ( int *one, int *two )
{
return (*one < *two) ? -1 :
(*one > *two) ? 1 : 0;
}

int main ( void )
{
int (*fcmp)() = &cmp_fcn;
qsort((void *)val, 5, sizeof(int), fcmp);
printf("%d %d %d %d %d\n",
val[0], val[1], val[2], val[3], val[4]);
return 0;
}
-----------------------------------

Regards,
Charles Sullivan

Nov 15 '05 #7
On Mon, 19 Sep 2005 23:36:43 GMT, Charles Sullivan
<cw******@triad.rr.com> wrote:
On Mon, 19 Sep 2005 20:12:17 +0000, Keith Thompson wrote:
Kenneth Brody <ke******@spamcop.net> writes:
Charles Sullivan wrote:

The library function 'qsort' is declared thus:
void qsort(void *base, size_t nmemb, size_t size,
int(*compar)(const void *, const void *));

If in my code I write:
int cmp_fcn(...);
int (*fcmp)() = &cmp_fcn;
qsort(..., fcmp);

then everything works. But if instead I code qsort as:

qsort(..., &cmp_fcn);

the compiler complains about incompatible pointer type.

qsort is expecting a pointer to a function which returns an int and
gets passed two const void * parameters. However, you are passing
it a pointer to a function which returns int and gets passed a
variable number of unknown parameters.
No, he's not. A function that takes a variable number of arguments
must have at least one named argument before the "...". Note also his
use of "..." in the call to qsort. The ellipsis isn't being used as C
syntax; it's just an ellipsis.

He's just not showing us exactly what he's doing -- which means we
can't guess what the problem is.


Mea culpa. I had forgotten about the C syntax and used "..." to
represent stuff I _thought_ irrelevant to the question.

Here's an example which illustrates the way I've been using
qsort:
-------------------------------------
#include <stdio.h>
#include <stdlib.h>

int val[] = { 3, 2, 1, 4, 5 };

int cmp_fcn ( int *one, int *two )


cmp_fcn has the wrong type for a function whose address is to be
passed to qsort. The two parameters need to be const void *.
{
return (*one < *two) ? -1 :
(*one > *two) ? 1 : 0;
}

int main ( void )
{
int (*fcmp)() = &cmp_fcn;
fcmp has the wrong type for a pointer to be used as an argument to
qsort. fcmp is a pointer to function (returning int) about whose
arguments (type and quantity) absolutely nothing is known. qsort
needs a pointer to function returning int about whose arguments
everything is known.
qsort((void *)val, 5, sizeof(int), fcmp);
printf("%d %d %d %d %d\n",
val[0], val[1], val[2], val[3], val[4]);
return 0;
}


Try increasing the warning level on your compiler.
<<Remove the del for email>>
Nov 15 '05 #8
Charles Sullivan <cw******@triad.rr.com> writes:
On Mon, 19 Sep 2005 20:12:17 +0000, Keith Thompson wrote:
Kenneth Brody <ke******@spamcop.net> writes:
Charles Sullivan wrote:
The library function 'qsort' is declared thus:
void qsort(void *base, size_t nmemb, size_t size,
int(*compar)(const void *, const void *));

If in my code I write:
int cmp_fcn(...);
int (*fcmp)() = &cmp_fcn;
qsort(..., fcmp);

then everything works. But if instead I code qsort as:

qsort(..., &cmp_fcn);

the compiler complains about incompatible pointer type.

qsort is expecting a pointer to a function which returns an int and
gets passed two const void * parameters. However, you are passing
it a pointer to a function which returns int and gets passed a
variable number of unknown parameters.


No, he's not. A function that takes a variable number of arguments
must have at least one named argument before the "...". Note also his
use of "..." in the call to qsort. The ellipsis isn't being used as C
syntax; it's just an ellipsis.

He's just not showing us exactly what he's doing -- which means we
can't guess what the problem is.


Mea culpa. I had forgotten about the C syntax and used "..." to
represent stuff I _thought_ irrelevant to the question.

Here's an example which illustrates the way I've been using
qsort:
-------------------------------------
#include <stdio.h>
#include <stdlib.h>

int val[] = { 3, 2, 1, 4, 5 };

int cmp_fcn ( int *one, int *two )
{
return (*one < *two) ? -1 :
(*one > *two) ? 1 : 0;
}

int main ( void )
{
int (*fcmp)() = &cmp_fcn;
qsort((void *)val, 5, sizeof(int), fcmp);
printf("%d %d %d %d %d\n",
val[0], val[1], val[2], val[3], val[4]);
return 0;
}
-----------------------------------


Yup, the stuff you deleted was exactly what's relevant to the problem.

The "compar" argument to qsort must be a pointer to a function taking
two arguments of type "const void *" and returning a result of type
int. You're providing a pointer to a function taking two arguments of
type int* and returning a result of type int. The types are
incompatible.

You need to declare your cmp_fcn() function as:

int cmp_fcn(const void *one, const void *2);

Here's a modified version of your program:

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

int val[] = { 3, 2, 1, 4, 5 };

int cmp_fcn ( const void *one, const void *two )
{
return (*(int*)one < *(int*)two) ? -1 :
(*(int*)one > *(int*)two) ? 1 : 0;
}

int main ( void )
{
qsort(val,
sizeof val / sizeof val[0],
sizeof val[0],
cmp_fcn);
printf("%d %d %d %d %d\n",
val[0], val[1], val[2], val[3], val[4]);
return 0;
}

Note that I've changed all four arguments to qsort().

For the first argument, I dropped the cast to void*. As long as
qsort's prototype is visible (which it is, since you have the
"#include <stdlib.h>", the conversion is done implicitly.

For the second argument, I compute the size of the val array rather
than assuming it's 5. This lets you change the size without having
to modify the call.

Similarly, the third argument is "sizeof val[0]" rather than
"sizeof(int)", allowing you to change the type of the array without
changing the qsort call (though you would have to change the
cmp_fcn function).

Finally, I pass the name of the function directly to qsort() rather
than storing it in a variable. The name of a function is implicitly
converted to a pointer-to-function in most contexts. (The exceptions
are the argument to sizeof, which makes "sizeof func" illegal rather
than yielding the size of a function pointer, and the argument to a
unary "&" operator, which makes &func nearly equivalent to func.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #9
On Mon, 19 Sep 2005 17:44:30 -0700, Barry Schwarz wrote:
On Mon, 19 Sep 2005 23:36:43 GMT, Charles Sullivan
<cw******@triad.rr.com> wrote:
On Mon, 19 Sep 2005 20:12:17 +0000, Keith Thompson wrote:
Kenneth Brody <ke******@spamcop.net> writes:
Charles Sullivan wrote:
>
> The library function 'qsort' is declared thus:
> void qsort(void *base, size_t nmemb, size_t size,
> int(*compar)(const void *, const void *));
>
> If in my code I write:
> int cmp_fcn(...);
> int (*fcmp)() = &cmp_fcn;
> qsort(..., fcmp);
>
> then everything works. But if instead I code qsort as:
>
> qsort(..., &cmp_fcn);
>
> the compiler complains about incompatible pointer type.

qsort is expecting a pointer to a function which returns an int and
gets passed two const void * parameters. However, you are passing
it a pointer to a function which returns int and gets passed a
variable number of unknown parameters.

No, he's not. A function that takes a variable number of arguments
must have at least one named argument before the "...". Note also his
use of "..." in the call to qsort. The ellipsis isn't being used as C
syntax; it's just an ellipsis.

He's just not showing us exactly what he's doing -- which means we
can't guess what the problem is.


Mea culpa. I had forgotten about the C syntax and used "..." to
represent stuff I _thought_ irrelevant to the question.

Here's an example which illustrates the way I've been using
qsort:
-------------------------------------
#include <stdio.h>
#include <stdlib.h>

int val[] = { 3, 2, 1, 4, 5 };

int cmp_fcn ( int *one, int *two )


cmp_fcn has the wrong type for a function whose address is to be
passed to qsort. The two parameters need to be const void *.
{
return (*one < *two) ? -1 :
(*one > *two) ? 1 : 0;
}

int main ( void )
{
int (*fcmp)() = &cmp_fcn;


fcmp has the wrong type for a pointer to be used as an argument to
qsort. fcmp is a pointer to function (returning int) about whose
arguments (type and quantity) absolutely nothing is known. qsort
needs a pointer to function returning int about whose arguments
everything is known.
qsort((void *)val, 5, sizeof(int), fcmp);
printf("%d %d %d %d %d\n",
val[0], val[1], val[2], val[3], val[4]);
return 0;
}


Try increasing the warning level on your compiler.


OK, if I change the function as suggested by you all to:
--------------------
int cmp_fcn ( const void *x1, const void *x2 )
{
const int *one = x1; const int *two = x2;
return (*one < *two) ? -1 :
(*one > *two) ? 1 : 0;
}
--------------------
then it works both ways. (Thanks guys.)

But I've yet to find a (gcc-3.2.2-5) compiler warning level that
grunts even a little with my original (working) code. As far as
I can remember I've used that syntax on a variety of Unix-like
systems over the years and never got a warning about it.

Thanks for all your help.

Regards,
Charles Sullivan
Nov 15 '05 #10
On Tue, 20 Sep 2005 00:46:40 +0000, Keith Thompson wrote:
Charles Sullivan <cw******@triad.rr.com> writes:
On Mon, 19 Sep 2005 20:12:17 +0000, Keith Thompson wrote:
Kenneth Brody <ke******@spamcop.net> writes:
Charles Sullivan wrote:
> The library function 'qsort' is declared thus:
> void qsort(void *base, size_t nmemb, size_t size,
> int(*compar)(const void *, const void *));
>
> If in my code I write:
> int cmp_fcn(...);
> int (*fcmp)() = &cmp_fcn;
> qsort(..., fcmp);
>
> then everything works. But if instead I code qsort as:
>
> qsort(..., &cmp_fcn);
>
> the compiler complains about incompatible pointer type.

qsort is expecting a pointer to a function which returns an int and
gets passed two const void * parameters. However, you are passing
it a pointer to a function which returns int and gets passed a
variable number of unknown parameters.

No, he's not. A function that takes a variable number of arguments
must have at least one named argument before the "...". Note also his
use of "..." in the call to qsort. The ellipsis isn't being used as C
syntax; it's just an ellipsis.

He's just not showing us exactly what he's doing -- which means we
can't guess what the problem is.


Mea culpa. I had forgotten about the C syntax and used "..." to
represent stuff I _thought_ irrelevant to the question.

Here's an example which illustrates the way I've been using
qsort:
-------------------------------------
#include <stdio.h>
#include <stdlib.h>

int val[] = { 3, 2, 1, 4, 5 };

int cmp_fcn ( int *one, int *two )
{
return (*one < *two) ? -1 :
(*one > *two) ? 1 : 0;
}

int main ( void )
{
int (*fcmp)() = &cmp_fcn;
qsort((void *)val, 5, sizeof(int), fcmp);
printf("%d %d %d %d %d\n",
val[0], val[1], val[2], val[3], val[4]);
return 0;
}
-----------------------------------


Yup, the stuff you deleted was exactly what's relevant to the problem.

The "compar" argument to qsort must be a pointer to a function taking
two arguments of type "const void *" and returning a result of type
int. You're providing a pointer to a function taking two arguments of
type int* and returning a result of type int. The types are
incompatible.

You need to declare your cmp_fcn() function as:

int cmp_fcn(const void *one, const void *2);

Here's a modified version of your program:

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

int val[] = { 3, 2, 1, 4, 5 };

int cmp_fcn ( const void *one, const void *two )
{
return (*(int*)one < *(int*)two) ? -1 :
(*(int*)one > *(int*)two) ? 1 : 0;
}

int main ( void )
{
qsort(val,
sizeof val / sizeof val[0],
sizeof val[0],
cmp_fcn);
printf("%d %d %d %d %d\n",
val[0], val[1], val[2], val[3], val[4]);
return 0;
}

Note that I've changed all four arguments to qsort().

For the first argument, I dropped the cast to void*. As long as
qsort's prototype is visible (which it is, since you have the
"#include <stdlib.h>", the conversion is done implicitly.

For the second argument, I compute the size of the val array rather
than assuming it's 5. This lets you change the size without having
to modify the call.

Similarly, the third argument is "sizeof val[0]" rather than
"sizeof(int)", allowing you to change the type of the array without
changing the qsort call (though you would have to change the
cmp_fcn function).

Finally, I pass the name of the function directly to qsort() rather
than storing it in a variable. The name of a function is implicitly
converted to a pointer-to-function in most contexts. (The exceptions
are the argument to sizeof, which makes "sizeof func" illegal rather
than yielding the size of a function pointer, and the argument to a
unary "&" operator, which makes &func nearly equivalent to func.)


Thanks Keith. I usually do what you've suggested in my actual
code, except for argument 3 (too easy to forget to change the
cmp-fcn function and start getting weird errors). And my
inability to follow your suggestion for argument 4 is what
prompted my original query.

Regards,
Charles Sullivan

Nov 15 '05 #11
Charles Sullivan wrote:
int cmp_fcn ( int *one, int *two )


This is incorrect. The prototype for qsort demands that the
comparison function be of type
int cmp_fcn(const void *e1, const void *e2);

See my post
Message-ID: <PT***************@newsread1.news.atl.earthlink.ne t>
Date: Mon, 19 Sep 2005 17:50:39 GMT

for the correct way of doing this.
Nov 15 '05 #12
Charles Sullivan wrote on 20/09/05 :
Here's an example which illustrates the way I've been using
qsort:
-------------------------------------
#include <stdio.h>
#include <stdlib.h>

int val[] = { 3, 2, 1, 4, 5 };

int cmp_fcn ( int *one, int *two )
Wrong prototype.
{
return (*one < *two) ? -1 :
(*one > *two) ? 1 : 0;
Complicated. A simple difference is enough. The 3 domains are <0, 0, >0
}

int main ( void )
{
int (*fcmp)() = &cmp_fcn;
Useless pointer to function masking the parameters error.
qsort((void *)val, 5, sizeof(int), fcmp);
USeless cast. Now, I have a 18 values array of doubles. What do I do ?
printf("%d %d %d %d %d\n",
val[0], val[1], val[2], val[3], val[4]);
Now, I have a 18 values array of ints. What do I do ?
return 0;
}
-----------------------------------


stay stright, simple and conforming:

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

#define NELEM(a) (sizeof (a) / sizeof *(a))

static int cmp_fcn (void const *one, void const *two)
{
int const *p_one = one;
int const *p_two = two;

return *p_one - *p_two;
}

static void print (int const *a, size_t n)
{
size_t i;
for (i = 0; i < n; i++)
{
printf ("%d ", a[i]);
}
printf ("\n");
}

int main (void)
{
int val[] =
{3, 2, 5, 1, 4};

print (val, NELEM (val));
qsort (val, NELEM (val), sizeof *val, cmp_fcn);
print (val, NELEM (val));

return 0;
}

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Clearly your code does not meet the original spec."
"You are sentenced to 30 lashes with a wet noodle."
-- Jerry Coffin in a.l.c.c++
Nov 15 '05 #13
"Emmanuel Delahaye" <em***@YOURBRAnoos.fr> writes:
Charles Sullivan wrote on 20/09/05 : [...]
int cmp_fcn ( int *one, int *two )


Wrong prototype.


Yes.
{
return (*one < *two) ? -1 :
(*one > *two) ? 1 : 0;


Complicated. A simple difference is enough. The 3 domains are <0, 0, >0


No. Subtraction invokes undefined behavior on overflow.
stay stright, simple and conforming:

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

#define NELEM(a) (sizeof (a) / sizeof *(a))

static int cmp_fcn (void const *one, void const *two)
{
int const *p_one = one;
int const *p_two = two;

return *p_one - *p_two;
}

static void print (int const *a, size_t n)
{
size_t i;
for (i = 0; i < n; i++)
{
printf ("%d ", a[i]);
}
printf ("\n");
}

int main (void)
{
int val[] =
{3, 2, 5, 1, 4};

print (val, NELEM (val));
qsort (val, NELEM (val), sizeof *val, cmp_fcn);
print (val, NELEM (val));

return 0;
}


Try changing the initial value of val to

{3, 2, 5, 1, 4, INT_MAX, INT_MIN}

(with "#include <limits.h", of course).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #14
On Tue, 20 Sep 2005 19:40:21 +0000, Keith Thompson wrote:
"Emmanuel Delahaye" <em***@YOURBRAnoos.fr> writes:
Charles Sullivan wrote on 20/09/05 :

[...]
int cmp_fcn ( int *one, int *two )


Wrong prototype.


Yes.
{
return (*one < *two) ? -1 :
(*one > *two) ? 1 : 0;


Complicated. A simple difference is enough. The 3 domains are <0, 0, >0


No. Subtraction invokes undefined behavior on overflow.
stay stright, simple and conforming:

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

#define NELEM(a) (sizeof (a) / sizeof *(a))

static int cmp_fcn (void const *one, void const *two)
{
int const *p_one = one;
int const *p_two = two;

return *p_one - *p_two;
}

static void print (int const *a, size_t n)
{
size_t i;
for (i = 0; i < n; i++)
{
printf ("%d ", a[i]);
}
printf ("\n");
}

int main (void)
{
int val[] =
{3, 2, 5, 1, 4};

print (val, NELEM (val));
qsort (val, NELEM (val), sizeof *val, cmp_fcn);
print (val, NELEM (val));

return 0;
}


Try changing the initial value of val to

{3, 2, 5, 1, 4, INT_MAX, INT_MIN}

(with "#include <limits.h", of course).


No need to nitpick the inner details - the code I posted was meant
only to illustrate the problem as simply as possible. (My first
attempt (with the "...") turned out to mask the real problem.)

Regards,
Charles Sullivan
Nov 15 '05 #15
On Tue, 20 Sep 2005 06:26:45 +0000, Martin Ambuhl wrote:
Charles Sullivan wrote:
int cmp_fcn ( int *one, int *two )


This is incorrect. The prototype for qsort demands that the
comparison function be of type
int cmp_fcn(const void *e1, const void *e2);

See my post
Message-ID: <PT***************@newsread1.news.atl.earthlink.ne t>
Date: Mon, 19 Sep 2005 17:50:39 GMT

for the correct way of doing this.


Yes, I followed your example and resolved the problem. Many thanks
for taking the time to respond and to generate the example.

Regards,
Charles Sullivan

Nov 15 '05 #16
Charles Sullivan <cw******@triad.rr.com> writes:
On Tue, 20 Sep 2005 19:40:21 +0000, Keith Thompson wrote:
"Emmanuel Delahaye" <em***@YOURBRAnoos.fr> writes:
Charles Sullivan wrote on 20/09/05 : [...]
int cmp_fcn ( int *one, int *two )

Wrong prototype.


Yes.
{
return (*one < *two) ? -1 :
(*one > *two) ? 1 : 0;

Complicated. A simple difference is enough. The 3 domains are <0, 0, >0


No. Subtraction invokes undefined behavior on overflow.

[...] No need to nitpick the inner details - the code I posted was meant
only to illustrate the problem as simply as possible. (My first
attempt (with the "...") turned out to mask the real problem.)


The nitpick was directed at what Emmanuel Delahaye posted, not at you
(and neither of you should take it personally). Questions here often
spawn lengthy discussion, sometimes raising points that have little to
do with the original question.

We comp.lang.c regulars are pathologically incapable of leaving an
error uncorrected. This is, IMHO, a good thing.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #17
Keith Thompson wrote on 21/09/05 :
We comp.lang.c regulars are pathologically incapable of leaving an
error uncorrected. This is, IMHO, a good thing.


Indeed. c.l.c. is my main source of C knowledge.
--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

..sig under repair
Nov 15 '05 #18

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

Similar topics

58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
4
by: Vijai Kalyan | last post by:
I was decomposing a task into different policies. Essentially, there is a general option obtained from a server and user options obtained from configuration variables. The two options are...
3
by: Goh, Yong Kwang | last post by:
I'm trying to create a function that given a string, tokenize it and put into a dynamically-sized array of char* which is in turn also dynamically allocated based on the string token length. I...
6
by: bob_jenkins | last post by:
{ const void *p; (void)memset((void *)p, ' ', (size_t)10); } Should this call to memset() be legal? Memset is of type void *memset(void *, unsigned char, size_t) Also, (void *) is the...
9
by: Juggernaut | last post by:
I am trying to create a p_thread pthread_create(&threads, &attr, Teste, (void *)var); where var is a char variable. But this doesnt't work, I get this message: test.c:58: warning: cast to pointer...
11
by: truckaxle | last post by:
I am trying to pass a slice from a larger 2-dimensional array to a function that will work on a smaller region of the array space. The code below is a distillation of what I am trying to...
7
by: Jeff K | last post by:
Can you pass an int array by reference to a function and modify selective elements? Here is my code: #include <stdio.h> #define COLUMNSIZE 30 #define ASIZE 5...
7
by: Jake Thompson | last post by:
Hello I created a DLL that has a function that is called from my main c program. In my exe I first get a a pointer to the address of the function by using GetProcAddress and on the dll side I...
3
by: sd2004 | last post by:
I am still learning, could someone show/explain to me how to fix the error. I can see it is being wrong but do not know how to fix. could you also recommend a book that I can ref. to ?...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.