1

I am trying to build a virtual function table. In an array I am storing the string(function name) and address of the function itself. I have googled for it. I was unable to find the solution?

char *arr[][] = { 
                 { "add", 0 },
                 { "sub", 0 },
                 { "div", 0 }
                };
int (*fp[3])(int, int) = {NULL};

fp[0] = &add;
fp[1] = ⊂
fp[2] = ÷
for(i=0; i<3; i++) {
    arr[0][i] = fp[i];
}

I am not able to store the address in the array. Could anyone have a look at my code?

BenMorel
  • 34,448
  • 50
  • 182
  • 322

2 Answers2

1
#include <stdio.h>

#define F(f_name, op) int f_name(int a, int b) { return a op b; }

F(add, +)
F(sub, -)
F(div, /)
//note : div function already exists in <stdlib.h>
enum { ADD, SUB, DIV};

struct vf {
    char *name;
    int (*fp)(int, int);
} vft[] = {
    { "add", add },
    { "sub", sub },
    { "div", div }
};

int main(void){
    printf("%d\n", (vft[SUB].fp)(5,2));//3
    return 0;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
  • Good answer. (illustrates specifically how to improve OP code to answer question) - Would be even better with some descriptive comments. – ryyker Jul 20 '14 at 18:52
  • Defining your own `div` causes undefined behaviour even if you didn't include `stdlib.h` and didn't call it. An easy fix is to prepend a unique string to the function name. – M.M Jul 21 '14 at 01:59
  • I wrote a note already. Supposed to be brought to light in the linker perhaps if there is a collision. – BLUEPIXY Jul 21 '14 at 02:23
0

What you need to do is create an array of structs, aka creating your own abstract data type, which contains one item per entry, which contains all the possible data you could need. In the code you posted, you were creating a 2d char array and trying to store mixed data in it (not a good idea).

In your case, at the minimum, you need a function pointer and a function string/name. I also added a "function symbol", to help keep any possible string output contained within the struct.

I've included a sample of a previous, similar such exercise I've already done in the past.

Please note that in this example, it is using integer division. If you're not already familiar with the pitfalls of such an operation, you should really read up on it. I've included a link to a good read on it in the References section at the bottom of my post.

Good luck!

Code Listing


/*******************************************************************************
 * Preprocessor Directives
 ******************************************************************************/
#include <stdio.h>
#define NUM_FCNS  (3)
/*******************************************************************************
 * Abstract Data Types
 ******************************************************************************/
/* Type alias for a pointer to a function that returns an int and takes
 * two int variables as arguments
 */
typedef int (*fp)(int, int);

typedef struct fcnEntry_t {
   fp          fcnPtr;
   const char* fcnName;
   const char  fcnSymbol;
} fcnEntry_t;

/*******************************************************************************
 * Function Protocols
 ******************************************************************************/
int add(int a, int b);
int sub(int a, int b);
int div(int a, int b);

/*******************************************************************************
 * Function Definitions
 ******************************************************************************/
int add(int a, int b) {
   return (a+b);
}

int sub(int a, int b) {
   return (a-b);
}

int div(int a, int b) {
   int ret;
   if (b == 0) {
      ret = 0;
   } else {
      ret = a / b;
   }
   return ret;
}

/*******************************************************************************
 * Main program entry point
 ******************************************************************************/
int main(void) {
   int i, a, b;
   /* Populate list of function pointer entries */
   fcnEntry_t  fcnTable[NUM_FCNS] = {
      {add, "addition",    '+'},
      {sub, "subtraction", '-'},
      {div, "division",    '/'}
   };

   /* Test all function table entries and log output/results */
   for (i=0, a=4, b=5; i<NUM_FCNS; i++) {
      printf("Function Name:%s a:%d b:%d\n", fcnTable[i].fcnName, a, b);
      printf("Result of %d %c %d = %d\n", a, fcnTable[i].fcnSymbol, b, fcnTable[i].fcnPtr(a,b));
   }

   /* Done */
   return 0;
}

Sample Run


Function Name:addition a:4 b:5
Result of 4 + 5 = 9
Function Name:subtraction a:4 b:5
Result of 4 - 5 = -1
Function Name:division a:4 b:5
Result of 4 / 5 = 0

References


  1. What is the behavior of integer division in C?, Accessed 2014-07-20, <https://stackoverflow.com/questions/3602827/what-is-the-behavior-of-integer-division-in-c>
Community
  • 1
  • 1
Cloud
  • 18,753
  • 15
  • 79
  • 153