Article View: comp.lang.c.moderated
Article #2487Re: Pointers to functions
From: Jason A. Farque'
Date: Thu, 04 Apr 1996 00:56
Date: Thu, 04 Apr 1996 00:56
82 lines
3973 bytes
3973 bytes
On 1 Apr 1996 07:20:30 -0600, Lloyd Hopkins <Lloyd@bomber.demon.co.uk>
wrote:
>Hi,
>
>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? 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? I'd also appreciate it if anyone could direct me to any more
>sources of information on the topics covered in 5.11 and 5.12.
>
>Thanks in advance for any help,
>
>Lloyd
Lloyd,
I can give you an example of why *I* use pointers to functions. There
are many different reasons, of course.
As an embedded systems programmer, I'm always writing code to handle
user interfaces of one kind or another. Often these user interfaces
have an LCD display that has one or two lines of text, a button or
two, etc. Here's a fairly typical construct of mine... Comments from
passersby are welcome...
typedef struct {
char *LineOne;
char *LineTwo;
void (* Function)(unsigned char);
}LCDMENUSTRUCT;
LCDMENUSTRUCT LcdMenu[] = {
{ "1: USING GPS?:", "Left button changes", MenuToggleGPS },
{ "2: FILES -> PC", "Left button begins", MenuSendRecordings },
{ "3: YOUR TIMEZONE:", "Left button changes", MenuSetTimeZone },
{ "4: SET CLOCK IN TOOL", "Left button begins", MenuSetClock },
{ "5: BATTERY CAL:", "Left button changes", MenuCalBatteryOffset },
{ "6: BATT LOW AT:", "Left button begins", MenuSetBatteryLevel },
{ "7: SERIAL NUMBER:", "Left button changes", MenuSetSerNum },
{ "8: RESET FILE MEMORY", "Left button begins", MenuRstFileMem },
{ "9: RESTORE DEFAULTS", "Left button begins", MenuSaneSystem }
};
I've stripped out some of the structure members to simplify this
example, but what we've got here is a structure LCDMENUSTRUCT that
contains two pointers to text strings "LineOne" and "LineTwo". These
are obviously displayed on the LCD display itself... The third
structure member is a pointer to a function (the function names
themselves). As the user presses the RIGHT button (there are only
two), I increment an offset number that points into the structure and
redisplay the text. I increment and check to see if the offset number
goes beyond the end of the stucture with this code:
if( ++MenuOffset > ((sizeof(LcdMenu)/sizeof(LCDMENUSTRUCT))-1) ) {
MenuOffset = 0;
}
However, if the user presses the LEFT button I make a function call
that looks like this:
LcdMenu[MenuOffset].Function(DO_CODE);
This makes it very simple for me to add a menu item. I merely write a
function to do whatever the new menu item is, add a line to the
structure and I'm done. This allows me to have a fairly small LCD
menu "Engine" function that runs the menu system without modification
no matter how I jack around with the menu items themselves. For
instance, I can comment out a line or two of the LcdMenu structure and
those items simply "go away".
That's a real-world example... Hope it helps you understand the
usefullness of pointers to functions... They're cool. When you need
'em, you need 'em bad.
Jason A. Farque'
CDI - Worldwide Suppliers of Pipeline Pig Location & Tracking Equipment.
Tulsa, Oklahoma, USA
Technical Details
Message-ID:
<4jv6or$cp2@solutions.solon.com>Path:
arc1-ca.pugleaf.net!solon.com!not-for-mailReferences:
<4jol6u$1sn@solutions.solon.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail From: farque@ion3.ionet.net (Jason A. Farque') Newsgroups: comp.lang.c.moderated Subject: Re: Pointers to functions Date: 3 Apr 1996 18:56:59 -0600 Organization: CDI Sender: clc@solutions.solon.com Approved: clc@solutions.solon.com Message-ID: <4jv6or$cp2@solutions.solon.com> References: <4jol6u$1sn@solutions.solon.com> NNTP-Posting-Host: solutions.solon.com X-Newsreader: Forte Agent .99d/32.182