Functions within structures

5013 mdb at abcom.ATT.COM
Wed Nov 7 10:25:29 AEST 1990


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];
} 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() }
    } ;
 
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))();
    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