Path: utzoo!utgpu!watmath!clyde!att!ulysses!cjc
From: cjc@ulysses.homer.nj.att.com (Chris Calabrese[mav])
Newsgroups: comp.lang.c
Subject: Re: how widespread is this cpp bug?
Summary: pointers to functions
Keywords: cpp whitespace bug
Message-ID: <10924@ulysses.homer.nj.att.com>
Date: 2 Dec 88 15:23:30 GMT
References: <49179@pyramid.pyramid.com> <12967@duke.cs.duke.edu>
Organization: AT&T Bell Laboratories, Murray Hill
Lines: 58

In article <12967@duke.cs.duke.edu>, khera@romeo.cs.duke.edu (Vick Khera) writes:
| I have used this ``feature'' to simplify having to write a bunch of
| duplicate code with a macro. what i needed was a bunch of buttons that had
| a particular label and when pressed, would call the function with a name
| based on the button label. for example, the button labeled ``inc'' would
| call inc_proc().  the comment is used to delimit the tokens as far as the
| pre-processor is concerned, but when the compiler gets it, it needs to be
| one token.  how else would this macro be constructed? 
| 
| excerpts from a sunview application:
| 
| #define BUTTON_WIDTH 8	/* width for command buttons */
| #define cmd_button(fnc) \
| 	panel_create_item(bs_panel, PANEL_BUTTON, \
| 		PANEL_LABEL_IMAGE,	panel_button_image(bs_panel, \
| 						"fnc",BUTTON_WIDTH,0), \
| 		PANEL_LABEL_BOLD,	TRUE, \
| 		PANEL_NOTIFY_PROC,	fnc/**/_proc, \
| 		0)
| 
| main(argc,argv)
| int argc;
| char *argv[];
| {
| 
| [ bunches of window creating code delted... ]
| 
| 	cmd_button(ali);	/* create the actual buttons */
| 	cmd_button(rne);	/* rmm;next */
| 	cmd_button(rpr);	/* rmm;prev */
| 
| [ bunches more code deleted. ]

You do this with pointers to functions of course.

void	cmd_button(char	*label, void	(*function)())
	{
	panel_create_item(bs_panel, PANEL_BUTTON,
		PANEL_LABEL_IMAGE,
		panel_button_image(bs_panel, label, BUTTON_WIDTH,0),
		PANEL_LABEL_BOLD,	TRUE,
		PANEL_NOTIFY_PROC,	function,
		0)

main() {
	extern	void	ali_proc();
	...
	cmd_button("ali", ali_proc);
	...
	}

You could also do it as a macro, or you could also use
an optimizing compiler which will put the call to cmd_button
inline if you tell it to.
-- 
	Christopher J. Calabrese
	AT&T Bell Laboratories
	att!ulysses!cjc		cjc@ulysses.att.com