Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //the original code is not by me (but it is without params)
- #include <cstdio>
- #include <cstdlib>
- #include <iostream>
- /* These are your handler functions */
- void user_fn(char params[]) { printf("USER fn\n%s\n",params); }
- void pass_fn(char params[]) { printf("PASS fn\n%s\n",params); }
- /* Stores a C string together with a function pointer */
- typedef struct fn_table_entry {
- char *name;
- void (*fn)(char params[]);
- } fn_table_entry_t;
- /* These are the functions to call for each command */
- fn_table_entry_t fn_table[] = {{"USER", user_fn}, {"PASS", pass_fn}};
- /* fn_lookup is a function that returns the pointer to the function for the given name.
- Returns NULL if the function is not found or if the name is NULL. */
- void (*fn_lookup(const char *fn_name))(char params[]) {
- int i;
- if (!fn_name) {
- return NULL;
- }
- for (i = 0; i < sizeof(fn_table)/sizeof(fn_table[0]); ++i) {
- if (!strcmp(fn_name, fn_table[i].name)) {
- return fn_table[i].fn;
- }
- }
- return NULL;
- }
- int main() {
- fn_lookup("USER")("char");
- fn_lookup("PASS")("int");
- system("pause");
- return 0;
- }
- //example by user470379 without parameters
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement