446,190 Members | 839 Online
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

# trying to assign function pointers

 P: n/a Hi I am having some problems assigning function pointers. this works fine: int (*compar) () = increasing; qsort(arr, MAX, sizeof(arr[0]), compar); this doesn't: if (argv[1] == "decreasing") int (*compar) () = decreasing; if (argv[1] == "increasing") int (*compar) () = increasing; qsort(arr, MAX, sizeof(arr[0]), compar); any ideas here is the full code below #include #include #define MAX 20 int intcmp(const void *v1, const void *v2); int increasing(const void *v1, const void *v2); int decreasing(const void *v1, const void *v2); int (*compar)(const void *v1, const void *v2); main(int argc, char** argv) { int arr[MAX], count, key, *ptr, i, a ,b; printf("argc = %d\n", argc); for (i = 0; i < argc; i++) printf("argv[%d] = \"%s\"\n", i, argv[i]); int (*compar) () = increasing; //if (argv[1] == "decreasing") //int (*compar) () = increasing; //if (argv[1] == "increasing") //int (*compar) () = decreasing; for (count = 0; count < MAX; count++) scanf("%d", &arr[count]); qsort(arr, MAX, sizeof(arr[0]), compar); //qsort(arr, MAX, sizeof(arr[0]), increasing); for (count = 0; count < MAX; count++) printf("arr[%d] = %d.\n", count, arr[count]); } int intcmp(const void *v1, const void *v2) { return (*(int *)v1 - *(int *)v2); } int increasing(const void *v1, const void *v2) { // printf ("increasing\n"); return (*(int *)v1 - *(int *)v2); } int decreasing(const void *v1, const void *v2) { // printf ("decreasing\n"); return (*(int *)v2 - *(int *)v1); } Jun 9 '07 #1
17 Replies

 P: n/a merrittr wrote: Hi I am having some problems assigning function pointers. this works fine: int (*compar) () = increasing; qsort(arr, MAX, sizeof(arr[0]), compar); this doesn't: if (argv[1] == "decreasing") int (*compar) () = decreasing; if (argv[1] == "increasing") int (*compar) () = increasing; qsort(arr, MAX, sizeof(arr[0]), compar); You can't declare compar twice, try int (*compar)(); if (argv[1] == "decreasing") compar = decreasing; if (argv[1] == "increasing") compar = increasing; qsort(arr, MAX, sizeof(arr[0]), compar); -- Ian Collins. Jun 9 '07 #2

 P: n/a Ian Collins said: merrittr wrote: >Hi I am having some problems assigning function pointers.this works fine: int (*compar) () = increasing; qsort(arr, MAX, sizeof(arr[0]), compar);this doesn't:if (argv[1] == "decreasing") int (*compar) () = decreasing;if (argv[1] == "increasing") int (*compar) () = increasing;qsort(arr, MAX, sizeof(arr[0]), compar); You can't declare compar twice Yes, he can, but it won't do him any good. , try > int (*compar)(); Wouldn't he be better off with int (*compar)(const void *, const void *); ? > if (argv[1] == "decreasing") compar = decreasing; if (argv[1] == "increasing") compar = increasing; Wouldn't he be better off looking up strcmp? -- Richard Heathfield "Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk email: rjh at the above domain, - www. Jun 9 '07 #3

 P: n/a Richard Heathfield wrote: Ian Collins said: >merrittr wrote: >>Hi I am having some problems assigning function pointers.this works fine: int (*compar) () = increasing; qsort(arr, MAX, sizeof(arr[0]), compar);this doesn't:if (argv[1] == "decreasing") int (*compar) () = decreasing;if (argv[1] == "increasing") int (*compar) () = increasing;qsort(arr, MAX, sizeof(arr[0]), compar); You can't declare compar twice Yes, he can, but it won't do him any good. Not the way he did in the same scope. , try >int (*compar)(); Wouldn't he be better off with int (*compar)(const void *, const void *); ? He would. >if (argv[1] == "decreasing") compar = decreasing;if (argv[1] == "increasing") compar = increasing; Wouldn't he be better off looking up strcmp? See what happens when one writes C++ all day? -- Ian Collins. Jun 9 '07 #4

 P: n/a Ian Collins said: Richard Heathfield wrote: >Ian Collins said: >>merrittr wrote:Hi I am having some problems assigning function pointers.this works fine: int (*compar) () = increasing; qsort(arr, MAX, sizeof(arr[0]), compar);this doesn't:if (argv[1] == "decreasing") int (*compar) () = decreasing;if (argv[1] == "increasing") int (*compar) () = increasing;qsort(arr, MAX, sizeof(arr[0]), compar);You can't declare compar twice Yes, he can, but it won't do him any good. Not the way he did Yes, he can... in the same scope. ....but not in the same scope. Nit finally picked to both our satisfactions, I think. >>if (argv[1] == "decreasing") compar = decreasing;if (argv[1] == "increasing") compar = increasing; Wouldn't he be better off looking up strcmp? See what happens when one writes C++ all day? Heretic. I'm suspending your clc posting licence for DBL_MIN seconds. That'll larn ya. -- Richard Heathfield "Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk email: rjh at the above domain, - www. Jun 9 '07 #5

 P: n/a "Richard Heathfield" Richard Heathfield wrote: >>Ian Collins said:merrittr wrote:Hi I am having some problems assigning function pointers.>this works fine: int (*compar) () = increasing; qsort(arr, MAX, sizeof(arr[0]), compar);>this doesn't:if (argv[1] == "decreasing") int (*compar) () = decreasing;if (argv[1] == "increasing") int (*compar) () = increasing;qsort(arr, MAX, sizeof(arr[0]), compar);>You can't declare compar twiceYes, he can, but it won't do him any good. Not the way he did Yes, he can... >in the same scope. ...but not in the same scope. Nit finally picked to both our satisfactions, I think. >>>if (argv[1] == "decreasing") compar = decreasing;if (argv[1] == "increasing") compar = increasing;Wouldn't he be better off looking up strcmp? See what happens when one writes C++ all day? Heretic. I'm suspending your clc posting licence for DBL_MIN seconds. Don't you think this is slightly too short and mild a punishment? Interesting: I thought DBL_MIN to be in limits.h, but I found it in float.h, DBL_MAX being in both... Bye, Jojo Jun 9 '07 #6

 P: n/a thanks guys that works perfectly On Jun 9, 5:53 am, "Joachim Schmitz" wrote: "Richard Heathfield" >merrittr wrote:Hi I am having some problems assigning function pointers. >>>this works fine: int (*compar) () = increasing; qsort(arr, MAX, sizeof(arr[0]), compar); >>>this doesn't:if (argv[1] == "decreasing") int (*compar) () = decreasing;if (argv[1] == "increasing") int (*compar) () = increasing;qsort(arr, MAX, sizeof(arr[0]), compar); >>You can't declare compar twice >Yes, he can, but it won't do him any good. Not the way he did Yes, he can... in the same scope. ...but not in the same scope. Nit finally picked to both our satisfactions, I think. >>if (argv[1] == "decreasing") compar = decreasing;if (argv[1] == "increasing") compar = increasing; >Wouldn't he be better off looking up strcmp? See what happens when one writes C++ all day? Heretic. I'm suspending your clc posting licence for DBL_MIN seconds. Don't you think this is slightly too short and mild a punishment? Interesting: I thought DBL_MIN to be in limits.h, but I found it in float.h, DBL_MAX being in both... Bye, Jojo Jun 9 '07 #7

 P: n/a On Sat, 9 Jun 2007 13:53:44 +0200, "Joachim Schmitz"

 P: n/a "Jack Klein" Interesting: I thought DBL_MIN to be in limits.h, but I found it infloat.h,DBL_MAX being in both... Your implementation's

 P: n/a Joachim Schmitz wrote, On 10/06/07 20:47: "Jack Klein" On Sat, 9 Jun 2007 13:53:44 +0200, "Joachim Schmitz">Interesting: I thought DBL_MIN to be in limits.h, but I found it infloat.h,DBL_MAX being in both... Your implementation's

 P: n/a "Flash Gordon" "Jack Klein" >On Sat, 9 Jun 2007 13:53:44 +0200, "Joachim Schmitz"

 P: n/a Joachim Schmitz wrote, On 11/06/07 07:26: "Flash Gordon" Joachim Schmitz wrote, On 10/06/07 20:47: >>"Jack Klein" #define DBL_MAX "Hello World" int main(void) { puts(DBL_MAX); return 0; } Subject to typos/thinkos, anyway. -- Flash Gordon Jun 11 '07 #12

 P: n/a Flash Gordon said: [...] if you do not include float.h it is legal for you to unconditionally define DBL_MAX. I.e. the following is perfectly legal... #include #define DBL_MAX "Hello World" int main(void) { puts(DBL_MAX); return 0; } Subject to typos/thinkos, anyway. Such as the typo of omitting

 P: n/a "Flash Gordon" "Flash Gordon" >Joachim Schmitz wrote, On 10/06/07 20:47:"Jack Klein" [snip]>>Interesting: I thought DBL_MIN to be in limits.h, but I found it in>float.h,>DBL_MAX being in both...Your implementation's #define DBL_MAX "Hello World" Hmm, well, maybe I have to take that up to the compiler vendor then (who at the same time is my employer...) Bye, Jojo Jun 11 '07 #14

 P: n/a Richard Heathfield wrote, On 11/06/07 09:08: Flash Gordon said: >[...] if you do not include float.h it is legal foryou to unconditionally define DBL_MAX. I.e. the following is perfectlylegal...#include #define DBL_MAX "Hello World"int main(void){ puts(DBL_MAX); return 0;}Subject to typos/thinkos, anyway. Such as the typo of omitting

 P: n/a On Sat, 09 Jun 2007 23:06:39 +1200, Ian Collins wrote: Richard Heathfield wrote: Ian Collins said: merrittr wrote: >if (argv[1] == "decreasing") int (*compar) () = decreasing;if (argv[1] == "increasing") int (*compar) () = increasing;qsort(arr, MAX, sizeof(arr[0]), compar); You can't declare compar twice Yes, he can, but it won't do him any good. Not the way he did in the same scope. In C99, they're not in the same scope. Each if statement now has its own (sub)scope, and the two declarations are legal. Of course, RH won't consider this relevant.

 P: n/a David Thompson wrote: On Sat, 09 Jun 2007 23:06:39 +1200, Ian Collins wrote: >Richard Heathfield wrote: Ian Collins said:merrittr wrote: >>if (argv[1] == "decreasing") int (*compar) () = decreasing;if (argv[1] == "increasing") int (*compar) () = increasing;qsort(arr, MAX, sizeof(arr[0]), compar);You can't declare compar twice Yes, he can, but it won't do him any good. Not the way he did in the same scope. In C99, they're not in the same scope. Each if statement now has its own (sub)scope, and the two declarations are legal. In both C90 and C99, tbe attempted declarations are syntax errors. The if condition must be followed by a statement. A declaration is not a statement. Jul 1 '07 #17

 P: n/a Harald van D?k said: David Thompson wrote: >On Sat, 09 Jun 2007 23:06:39 +1200, Ian Collins>Richard Heathfield wrote:Ian Collins said:merrittr wrote: >>>if (argv[1] == "decreasing") int (*compar) () = decreasing;if (argv[1] == "increasing") int (*compar) () = increasing;qsort(arr, MAX, sizeof(arr[0]), compar);You can't declare compar twiceYes, he can, but it won't do him any good.Not the way he did in the same scope. In C99, they're not in the same scope. Each if statement now has itsown (sub)scope, and the two declarations are legal. In both C90 and C99, tbe attempted declarations are syntax errors. The if condition must be followed by a statement. A declaration is not a statement. See, this is why David Thompson is so useful. He notices things like this. My reply was incorrect: I am so used to braces around the 'body' of an if (because I always use them) that it seems I can see them even when they're not there. -- Richard Heathfield Email: -www. +rjh@ Google users: "Usenet is a strange place" - dmr 29 July 1999 Jul 1 '07 #18

### This discussion thread is closed

Replies have been disabled for this discussion.