Building menus from Arrays of Pointers to Functions

Posted on 2003-03-27
Medium Priority
Last Modified: 2010-04-15

I am using standard C to construct a menu. The menu will have several options
that call the appropriate function. If I want to build a menu with an array
and let the array contain pointers to the function and also assuming the
functions in general are of different types the I will run into trouble because the array only like things of the same type. How does one create something like this using <ordinary> C.Just looking for methodology. Thanks
Question by:ecrouse
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 10

Expert Comment

ID: 8220629
i haven't tested this, but i think you should just be able to

1) declare your array to be of type void* (the general pointer type).. but you might need to keep track of which type it really is in another array, and then cast it.. i'm not sure

2)to get the pointer to the functions just use address operator on it

3) then just populate your array

see what happens.
LVL 16

Expert Comment

ID: 8220809
I can imagine two possibilities. Either the functions are all very different, or they're not.

If the functions are very different, that is they return different kinds of things, and take different argument lists, then the advantage of putting a series of pointers to them in an array would appear to be lost. You are going to need some kind of switch anyway, in order to deal with the returns and/or argument lists.

If the functions are all very similar then the array problem disappears all together. I have code in which there is a series of function pointers in an array for a menu. Each of the functions returns void, and takes an integer as an argument. This works very well (in my situation anyway).
LVL 10

Expert Comment

ID: 8220880
you could also change your function definitions to all return void, then add an argument to each of them that is pointer to the return variable, and set that within the function instead of actually returning a value.


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.


Accepted Solution

honey_hamster earned 1000 total points
ID: 8221028
An example of ideas already hinted at by previous posts...

#include <stdio.h>

#define RETURN_INT   0
#define RETURN_LONG  1
#define RETURN_FLOAT 2
#define RETURN_CHAR  3

struct return_struct
  int   return_type;

    int   i;
    long  l;
    float f;
    char  c;
  } u;

void menu_option_0( struct return_struct *ret_ptr )
  int a = 5;

  ret_ptr->return_type = RETURN_INT;
  ret_ptr->u.i         = a;

void menu_option_1( struct return_struct *ret_ptr )
  long a = 5;

  ret_ptr->return_type = RETURN_LONG;
  ret_ptr->u.l         = a;

void menu_option_2( struct return_struct *ret_ptr )
  float a = 5.0;

  ret_ptr->return_type = RETURN_FLOAT;
  ret_ptr->u.f         = a;

void menu_option_3( struct return_struct *ret_ptr )
  char a = '5';

  ret_ptr->return_type = RETURN_CHAR;
  ret_ptr->u.c         = a;

typedef void (*menu_func)( struct return_struct *r );

menu_func menu_array[ 4 ] =

int get_menu_input( void )
  int i;

  printf( "Enter value " );
  scanf( "%d", &i );

  return( i );

void menu_handler( menu_func *handlers )
  int                  input;
  struct return_struct r;

  input  = get_menu_input();

  if( input < 4 )
    (handlers)[ input ](&r );

    switch( r.return_type )
      case RETURN_INT:
        printf( "%d\n", r.u.i );

      case RETURN_LONG:
        printf( "%ld\n", r.u.l );

      case RETURN_FLOAT:
        printf( "%f\n", r.u.f );

      case RETURN_CHAR:
        printf( "%c\n", r.u.c );

int main( void )
  menu_handler( menu_array );

  return( 0 );

Author Comment

ID: 8221149
Sweet Description/Example Honey_Hamster!

Author Comment

ID: 8221157
Sweet Description/Example Honey_Hamster?!

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question