C Map String to Function
Posted
by
Scriptonaut
on Stack Overflow
See other posts from Stack Overflow
or by Scriptonaut
Published on 2012-10-07T03:29:48Z
Indexed on
2012/10/07
3:37 UTC
Read the original article
Hit count: 148
So, I'm making a Unix minishell, and have come to a roadblock. I need to be able to execute built-in functions, so I made a function:
int exec_if_built_in(char **args)
It takes an array of strings(the first being the command, and the rest being arguments). For non built-in commands I simply use something like execvp, however I need to find a way to map the first string to a function. I was thinking of making two arrays, one of strings, and another with their corresponding function pointers. However, since many of these functions will be different(return and accept different things), this approach won't work. I also thought of making an array of structs with a name property and a function pointer property, however once again due to the varied nature of the functions I'll be using, this won't work.
So, what's the best way to execute a function based on the input of a string? How do I map a string to a certain function? I'm not very familiar with function pointers so I may be missing something.
Thank you guys for the help :)
© Stack Overflow or respective owner