473,503 Members | 1,700 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

trying to assign function pointers

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 <stdio.h>
#include <stdlib.h>

#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 1391
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
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
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
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.

<snip>
>>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
"Richard Heathfield" <rj*@see.sig.invalidschrieb im Newsbeitrag
news:hd******************************@bt.com...
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.

<snip>
>>>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
thanks guys that works perfectly


On Jun 9, 5:53 am, "Joachim Schmitz" <nospam.j...@schmitz-digital.de>
wrote:
"Richard Heathfield" <r...@see.sig.invalidschrieb im Newsbeitragnews:hd******************************@b t.com...
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.
<snip>
>>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
On Sat, 9 Jun 2007 13:53:44 +0200, "Joachim Schmitz"
<no*********@schmitz-digital.dewrote in comp.lang.c:

[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 <limits.his broken if it defines DBL_MAX,
unless it includes some mechanism to disable that definition when
compiling in strictly conforming mode.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Jun 10 '07 #8

"Jack Klein" <ja*******@spamcop.netschrieb im Newsbeitrag
news:43********************************@4ax.com...
On Sat, 9 Jun 2007 13:53:44 +0200, "Joachim Schmitz"
<no*********@schmitz-digital.dewrote in comp.lang.c:

[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 <limits.his broken if it defines DBL_MAX,
unless it includes some mechanism to disable that definition when
compiling in strictly conforming mode.
It does, sort of, with an #ifdef DBL_MAX

Bye, Jojo
Jun 10 '07 #9
Joachim Schmitz wrote, On 10/06/07 20:47:
"Jack Klein" <ja*******@spamcop.netschrieb im Newsbeitrag
news:43********************************@4ax.com...
>On Sat, 9 Jun 2007 13:53:44 +0200, "Joachim Schmitz"
<no*********@schmitz-digital.dewrote in comp.lang.c:

[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 <limits.his broken if it defines DBL_MAX,
unless it includes some mechanism to disable that definition when
compiling in strictly conforming mode.
It does, sort of, with an #ifdef DBL_MAX
It it was not defined before including limits.h but it is after then it
does not conform to the standard.
--
Flash Gordon
Jun 10 '07 #10
"Flash Gordon" <sp**@flash-gordon.me.ukschrieb im Newsbeitrag
news:c3************@news.flash-gordon.me.uk...
Joachim Schmitz wrote, On 10/06/07 20:47:
>"Jack Klein" <ja*******@spamcop.netschrieb im Newsbeitrag
news:43********************************@4ax.com.. .
>>On Sat, 9 Jun 2007 13:53:44 +0200, "Joachim Schmitz"
<no*********@schmitz-digital.dewrote in comp.lang.c:

[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 <limits.his broken if it defines DBL_MAX,
unless it includes some mechanism to disable that definition when
compiling in strictly conforming mode.
It does, sort of, with an #ifdef DBL_MAX

It it was not defined before including limits.h but it is after then it
does not conform to the standard.
That's why I said 'sort of'. I just checked again: float.h is doing the same
thing, so it doesn't matter which is included or in what order, si I guess
we're save here...

Bye, Jojo
Jun 11 '07 #11
Joachim Schmitz wrote, On 11/06/07 07:26:
"Flash Gordon" <sp**@flash-gordon.me.ukschrieb im Newsbeitrag
news:c3************@news.flash-gordon.me.uk...
>Joachim Schmitz wrote, On 10/06/07 20:47:
>>"Jack Klein" <ja*******@spamcop.netschrieb im Newsbeitrag
news:43********************************@4ax.com. ..
On Sat, 9 Jun 2007 13:53:44 +0200, "Joachim Schmitz"
<no*********@schmitz-digital.dewrote in comp.lang.c:

[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 <limits.his broken if it defines DBL_MAX,
unless it includes some mechanism to disable that definition when
compiling in strictly conforming mode.
It does, sort of, with an #ifdef DBL_MAX
It it was not defined before including limits.h but it is after then it
does not conform to the standard.
That's why I said 'sort of'. I just checked again: float.h is doing the same
thing, so it doesn't matter which is included or in what order, si I guess
we're save here...
No you are not because 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 <limits.h>

#define DBL_MAX "Hello World"

int main(void)
{
puts(DBL_MAX);
return 0;
}

Subject to typos/thinkos, anyway.
--
Flash Gordon
Jun 11 '07 #12
Flash Gordon said:

<snip>
[...] 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 <limits.h>

#define DBL_MAX "Hello World"

int main(void)
{
puts(DBL_MAX);
return 0;
}

Subject to typos/thinkos, anyway.
Such as the typo of omitting <stdio.h- not strictly a problem in C90
for this particular program, but I believe it moves your code outside
the common subset of C90 and C99.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jun 11 '07 #13
"Flash Gordon" <sp**@flash-gordon.me.ukschrieb im Newsbeitrag
news:41************@news.flash-gordon.me.uk...
Joachim Schmitz wrote, On 11/06/07 07:26:
>"Flash Gordon" <sp**@flash-gordon.me.ukschrieb im Newsbeitrag
news:c3************@news.flash-gordon.me.uk...
>>Joachim Schmitz wrote, On 10/06/07 20:47:
"Jack Klein" <ja*******@spamcop.netschrieb im Newsbeitrag
news:43********************************@4ax.com ...
On Sat, 9 Jun 2007 13:53:44 +0200, "Joachim Schmitz"
<no*********@schmitz-digital.dewrote in comp.lang.c:
>
[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 <limits.his broken if it defines DBL_MAX,
unless it includes some mechanism to disable that definition when
compiling in strictly conforming mode.
It does, sort of, with an #ifdef DBL_MAX
It it was not defined before including limits.h but it is after then it
does not conform to the standard.
That's why I said 'sort of'. I just checked again: float.h is doing the
same thing, so it doesn't matter which is included or in what order, si I
guess we're save here...

No you are not because 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 <limits.h>

#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
Richard Heathfield wrote, On 11/06/07 09:08:
Flash Gordon said:

<snip>
>[...] 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 <limits.h>

#define DBL_MAX "Hello World"

int main(void)
{
puts(DBL_MAX);
return 0;
}

Subject to typos/thinkos, anyway.

Such as the typo of omitting <stdio.h- not strictly a problem in C90
for this particular program, but I believe it moves your code outside
the common subset of C90 and C99.
It was a lackofthinko that would have been caught by the compiler as I
normally use it. At least I did not use a varidac function so it was
valid for C90 and K&R :-)

You are, of course, correct to point out the error.
--
Flash Gordon
Jun 11 '07 #15
On Sat, 09 Jun 2007 23:06:39 +1200, Ian Collins <ia******@hotmail.com>
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. <joke>Of course,
RH won't consider this relevant.</They are useless, since neither is
available to the attempted use. And as already noted, comparing
strings that way won't work as desired either, and that type is not as
exact (complete) as it could usefully be.

- formerly david.thompson1 || achar(64) || worldnet.att.net
Jul 1 '07 #16
David Thompson wrote:
On Sat, 09 Jun 2007 23:06:39 +1200, Ian Collins <ia******@hotmail.com>
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
Harald van D?k said:
David Thompson wrote:
>On Sat, 09 Jun 2007 23:06:39 +1200, Ian Collins
<ia******@hotmail.comwrote:
>>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.
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 <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jul 1 '07 #18

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

Similar topics

1
2388
by: tuko | last post by:
Hello kind people. I have the classes "point", "curve", "surface", "region" as shown below. Every class has a member function that returns a list of pointers to objects that comprise the *this...
4
2483
by: steflhermitte | last post by:
Dear cpp-ians, I am working with a structure struct segment { .... vector <meta_segment>::iterator it_Z; .... };
2
2287
by: Prashant | last post by:
Hello All, I am new to win32 programming,I was trying to get all the Subkeys under any Key. I have written a C program for achieving this, I am using WinNT Server . In this program ...
51
2496
by: Richard Hengeveld | last post by:
Hi all, I'm trying to understand how pointers for function parameters work. As I understand it, if you got a function like: void f(int *i) { *i = 0; }
4
2221
by: Deniz Bahar | last post by:
Hello, A couple days ago my friend (OOP guy) shows me what OOP was all about in C++. This morning I figured I can do pretty much the same thing with C (by putting function pointers in...
10
1495
by: Tom | last post by:
Hello, I've recently started trying to learn C (for the fun of it!). I have been using Perl and other languages for many years, but they were always very high level, this is giving me quite a...
4
1899
by: Garry Freemyer | last post by:
I'm trying to convert this macro to a c# function but I have a big problem. It's on the LEFT side of an assignment statement and I am extremely flustered over this one because I'm a little rusty...
7
2501
by: Edward Diener | last post by:
Since implement the assign operator for reference types eliminates the ability to assign a reference object to a reference variable of the same type or base class of that type, I assume that...
11
3754
by: skumar434 | last post by:
Hi everybody, I am faceing problem while assigning the memory dynamically to a array of structures . Suppose I have a structure typedef struct hom_id{ int32_t nod_de; int32_t hom_id;
0
7202
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
7084
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7278
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,...
1
6991
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5013
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1512
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.