Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!sol.ctr.columbia.edu!cica!news.cs.indiana.edu!att!emory!gatech!mcnc!rti!dg-rtp!gamecock!hagins From: hagins@gamecock.rtp.dg.com (Jody Hagins) Newsgroups: comp.lang.c Subject: Re: Functions within structures Keywords: structures Message-ID: <1990Nov8.152228.24407@dg-rtp.dg.com> Date: 8 Nov 90 15:22:28 GMT References: <2203@abcom.ATT.COM> Sender: usenet@dg-rtp.dg.com (Usenet Administration) Reply-To: hagins@gamecock.rtp.dg.com (Jody Hagins) Organization: Data General Corporation, Research Triangle Park, NC Lines: 109 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@abcom.ATT.COM>, mdb@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 |> #include |> #include |> #include |> |> #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