Basic and Advance C Question:
Download Job Interview Questions and Answers PDF
If I have a char * variable pointing to the name of a function ...
Answers:
Answer #1If I have a char * variable pointing to the name of a function, how can I call that function? Code like
extern int func(int, int);
char *funcname = "func";
int r = (*funcname)(1, 2);
or
r = (*(int (*)(int, int))funcname)(1, 2);
doesn't seem to work.
By the time a program is running, information about the names of its functions and variables (the ``symbol table'') is no longer needed, and may therefore not be available. The most straightforward thing to do, therefore, is to maintain that information yourself, with a correspondence table of names and function pointers:
int one_func(), two_func();
int red_func(), blue_func();
struct { char *name; int (*funcptr)(); } symtab[] = {
"one_func", one_func,
"two_func", two_func,
"red_func", red_func,
"blue_func", blue_func,
};
Then, search the table for the name, and call via the associated function pointer, with code like this:
#include <stddef.h>
int (*findfunc(char *name))()
{
int i;
for(i = 0; i < sizeof(symtab) / sizeof(symtab[0]); i++) {
if(strcmp(name, symtab[i].name) == 0)
return symtab[i].funcptr;
}
return NULL;
}
...
char *funcname = "one_func";
int (*funcp)() = findfunc(funcname);
if(funcp != NULL)
(*funcp)();
extern int func(int, int);
char *funcname = "func";
int r = (*funcname)(1, 2);
or
r = (*(int (*)(int, int))funcname)(1, 2);
doesn't seem to work.
By the time a program is running, information about the names of its functions and variables (the ``symbol table'') is no longer needed, and may therefore not be available. The most straightforward thing to do, therefore, is to maintain that information yourself, with a correspondence table of names and function pointers:
int one_func(), two_func();
int red_func(), blue_func();
struct { char *name; int (*funcptr)(); } symtab[] = {
"one_func", one_func,
"two_func", two_func,
"red_func", red_func,
"blue_func", blue_func,
};
Then, search the table for the name, and call via the associated function pointer, with code like this:
#include <stddef.h>
int (*findfunc(char *name))()
{
int i;
for(i = 0; i < sizeof(symtab) / sizeof(symtab[0]); i++) {
if(strcmp(name, symtab[i].name) == 0)
return symtab[i].funcptr;
}
return NULL;
}
...
char *funcname = "one_func";
int (*funcp)() = findfunc(funcname);
if(funcp != NULL)
(*funcp)();
Answer #2The callable functions should all have compatible argument and return types. (Ideally, the function pointers would also specify the argument types.)
It is sometimes possible for a program to read its own symbol table if it is still present, but it must first be able to find its own executable , and it must know how to interpret the symbol table (some Unix C libraries provide an nlist function for this purpose).
It is sometimes possible for a program to read its own symbol table if it is still present, but it must first be able to find its own executable , and it must know how to interpret the symbol table (some Unix C libraries provide an nlist function for this purpose).
Download C Programming Interview Questions And Answers
PDF
Previous Question | Next Question |
How can I manipulate individual bits? | How can I write data files which can be read on other machines with different word size, byte order, or floating point formats? |