how widespread is this cpp bug?

Chris Calabrese[mav] cjc at ulysses.homer.nj.att.com
Sat Dec 3 01:23:30 AEST 1988


In article <12967 at duke.cs.duke.edu>, khera at 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 at ulysses.att.com



More information about the Comp.lang.c mailing list