Functions within structures

Jody Hagins hagins at gamecock.rtp.dg.com
Fri Nov 9 02:22:28 AEST 1990


First off, this sounds like a class project, but since you
are not asking for a program, but specific help, I consider
this to be fine since you could find this in any C book.
I used to teach, and I would not have a problem with this type
of assistance.


In article <2203 at abcom.ATT.COM>, mdb at abcom.ATT.COM (5013 ) writes:
|> Hi, netlanders,
|>  
|>    I need help.  I am building a menu using structures and would like
|>    to be able to include the function to be run within the structure.
|>  
|>    Below is an example of what I would like to do.
|> ===========================================================================
|> /****************************************************************************
|> **
|> **      Program     : CH10.c
|> **      Author      : Michael D. Barnes
|> **      Date Created: November 04, 1990
|> **      Date Revised:
|> **      Notes       : This database manager will:
|> **
|> **                        ADD RECORD
|> **                        FIND RECORD
|> **                        QUIT
|> **
|> **      Globals used: MENU_ITEM -> Number of items in menu
|> **                    MAX_RECS  -> Maximum Records in database allowed
|> **                    sorted    -> Does database need to be sorted?
|> **                    phone_bk  -> structure phone book data record
|> **                    menu      -> structure menu data record
|> **
|> ****************************************************************************/
|>  
|> #include <stdio.h>
|> #include <conio.h>
|> #include <bios.h>
|> #include <ctype.h>
|>  
|> #define MENU_ITEM 2
|> #define MAX_RECS  6
|>  
|> int   add_rec();
|> int   find_rec();
|> int   quit();
|>  
|> unsigned	int	records=0;
|> typedef struct {
|>    char    key;
|>    int     x_row;
|>    int     y_row;
|>    char    msg[20];


      int     (*foo)();		/* You need to have this field */


|> } MENU;
|>  
|> MENU menu[MENU_ITEM+1] = {
|>     { '1',7,25,"Add Phone Number",add_rec() },
|>     { '2',9,25,"Find Phone number",find_rec() },
|>     { 'Q',12,25,"QUIT",quit() }
|>     } ;


Remove the ()'s from the above assignments of add_rec(), find_rec(),
and quit().


|>  
|> int menu_cnt=MENU_ITEM;        /* What menu option is used */
|>  
|> main(void)
|> {
|>  
|> int   menu_opt;
|> disp_menu(menu,menu_cnt);
|>  
|> while ( 1 )
|> {
|>  
|> menu_opt = select();
|> (*(menu[menu_opt].foo))();


This should work now.  However, keep in mind that menu[menu_opt].foo
is a pointer to a function of type int.  Don't try to mix
function pointer types.  Also, remember that you need to pass parms
in the ()'s if they are needed.


|>     gotoxy ( menu[menu_cnt].y_row,menu[menu_cnt].x_row );
|>     printf ( "%c. %s",menu[menu_cnt].key,menu[menu_cnt].msg );
|>     gotoxy ( menu[menu_cnt].y_row,menu[menu_cnt].x_row );
|> }
|> }
|> =======================================================================
|> Now, how do I assigned these functions to the structure (or is it posible)
|> and then access these same functions?  Any help will be greatly appreciated.
|>  
|>  
|> 
|> 			Thank for yor help in advance
|> 
|> 			Mike Barnes
|> 			(415) 224-3030



More information about the Comp.lang.c mailing list