🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c.moderated
Article #2490

Re: Pointers to functions

#2490
From: Miguel Carrasquer Vidal
Date: Thu, 04 Apr 1996 01:08
76 lines
3553 bytes
Lloyd Hopkins <Lloyd@bomber.demon.co.uk> wrote:

>I've recently been working my way through K&R (second edition) and whilst
>most of it is clear enough, I'm having trouble with 5.11, 5.12 (Pointers to
>Functions, Complicated Declarations) and I thought I'd ask here to see
>if anyone can help.  The main thing which is puzzling me, is just why
>you'd want to create a pointer to a function?  

Pointers to functions can be handy for a sorting algorithm, for
instance.  "The sorting algorithm is independent of the comparison
[and exchange] operations, so by passing different comparison [and
exchange] functions to it, we can arrange to sort by different
criteria."  That's pretty clear, I think.

>Another thing is that looking
>at the sort program in 5.11, there is a reference to a function called
>"comp", but I can't actually see any function called "comp" anywhere in
>the code?

"comp" is the name of a _pointer_ to a function.  The actual functions
called in this case are "numcmp" or "strcmp"...

The call to qsort (p. 119 in my edition) is:

   qsort((void **)lineptr, 0, nlines-1,
      (int (*)(void *, void *))(numeric ? numcmp : strcmp));

in other words, you pass a _pointer_ to the function "numcmp" or
"strcmp", depending on whether the sort should be numeric or
alphabetic.

Pointers to functions are really not that different from ordinary
variables.  When you declare say "int year", you know that you can
assign "year=1996;" and call "print_year(year);".  "print_year()"
doesn't have to know the value of the "year" variable, and should work
fine given the assignment "year=2000;" (or will it?).

The confusing thing I guess about function-pointer variables is that
you're "catching" a piece of code inside of a data variable, and pass
that around.  While the principle of "given this condition, do this,
otherwise do something else" is at the heart of any computer program
above the level of complexity of "hello, world!", in most cases what
you do is simply:

if(this)
	do_this();
else	do_that();

Pointers to functions allow a different approach.  Not one you need to
use every day, but one which can be very useful in things like
sorting, state machines, or anywhere your code can benefit from the
"added generality" [what I mean is that I can't explain when you
should use it: after a while you just find yourself in a given
situation and you say "I could use a function pointer here", and you
do].  It looks something like this:

void (*somefunction)(void);

if(this)
	somefunction=do_this;
else	somefunction=do_that;

somefunction();

The effect is exactly the same as in the piece of code above that
called do_this() or do_that() directly.  The difference is that you
now have this variable "somefunction" that you can call, assign to,
compare, pass around to another function (like qsort()), etc.


==
Miguel Carrasquer Vidal                     ~ ~
Amsterdam                   _____________  ~ ~
mcv@pi.net                 |_____________|||

========================== Ce .sig n'est pas une .cig
Technical Details
Message-ID: <4jv7ea$d24@solutions.solon.com>
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
References: <4jol6u$1sn@solutions.solon.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
From: mcv@pi.net (Miguel Carrasquer Vidal)
Newsgroups: comp.lang.c.moderated
Subject: Re: Pointers to functions
Date: 3 Apr 1996 19:08:26 -0600
Organization: Planet Internet
Sender: clc@solutions.solon.com
Approved: clc@solutions.solon.com
Message-ID: <4jv7ea$d24@solutions.solon.com>
References: <4jol6u$1sn@solutions.solon.com>
NNTP-Posting-Host: solutions.solon.com
X-Newsreader: Forte Free Agent 1.0.82