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?

7
  • 5
    You can't. Arrays are made to store only one kind of data. What you're looking for is structures. Commented Jul 20, 2014 at 15:03
  • 2
    Also, in your loop you're using the indexes wrong. It should probably be arr[i][1]. Commented Jul 20, 2014 at 15:04
  • Thanks Joachim. The array is of type char * so there is a chance of storing the integer pointer which is an address. Commented Jul 20, 2014 at 15:05
  • in the place of 0 i.e arr[0][0] the addresses should be stored Commented Jul 20, 2014 at 15:08
  • 1
    @user1624109 but function pointers are not of type char *, that's why you have to use an array of structures. Commented Jul 20, 2014 at 15:08

2 Answers 2

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;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Good answer. (illustrates specifically how to improve OP code to answer question) - Would be even better with some descriptive comments.
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.
I wrote a note already. Supposed to be brought to light in the linker perhaps if there is a collision.
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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.