Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Building menus from Arrays of Pointers to Functions

Posted on 2003-03-27
6
Medium Priority
?
203 Views
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
0
Comment
Question by:ecrouse
6 Comments
 
LVL 10

Expert Comment

by:substand
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.
0
 
LVL 16

Expert Comment

by:imladris
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).
0
 
LVL 10

Expert Comment

by:substand
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.

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 2

Accepted Solution

by:
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;

  union
  {
    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 ] =
{
  menu_option_0,
  menu_option_1,
  menu_option_2,
  menu_option_3
};


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 );
        break;

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

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

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

int main( void )
{
  menu_handler( menu_array );

  return( 0 );
}
0
 

Author Comment

by:ecrouse
ID: 8221149
Sweet Description/Example Honey_Hamster!
0
 

Author Comment

by:ecrouse
ID: 8221157
Sweet Description/Example Honey_Hamster?!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
Suggested Courses

564 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