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
- 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>