Reading from File

A

Aditya

Hi Guys,

It's not a very tricky questions .

int function_0()
{

}
main ()
{
table [function name][Function Pointer]
}

Example Table [Function_0][&Function_0]

I would like to know how to maintain the above table. I need to read
the "Function_0" from a file and then store the address of
"Function_0" in the table.

Thanks and Regards

Aditya
 
E

Ersek, Laszlo

I need to read the "Function_0" from a file and then store the address
of "Function_0" in the table.

If you're on a POSIX system: look up dlopen() and friends.

http://www.opengroup.org/onlinepubs/9699919799/functions/dlopen.html

Even if you use dlopen() and constrain yourself to POSIX platforms,
implementation-defined details remain:

"The class of files eligible for this operation and the manner of their
construction are implementation-defined. [...]"

Think "gcc -shared", SUN "cc -G", GNU libtool, and so on.

lacos
 
N

Nick Keighley

It's not a very tricky questions .

the answer might be though...

int function_0()

int function_0 (void)
{

}

main ()

int main (void)

main returns an int and you should say so
{
  table [function name][Function Pointer]

return 0;

main returns an int and you should return one

/* typedef to define a function pointer */
typedef int (*FuncPtr) (void);

int function_0 (void)
{
}

int function_1 (void)
{
}

int main (void)
{
FuncPtr table [] = {function_0, function_1};
return 0;
}

The above is just a table of function pointers

Example Table [Function_0][&Function_0]

I would like to know how to maintain the above table. I need to read
the "Function_0" from a file and then store the address of
"Function_0" in the table.

ah, you want to look up the names in a table. I've done the typedef
slightly differently to avoid hiding the "*" in the typedef. Doing so
can lead to hard to understand code.

/* typedef used to form function pointer */
typedef int Func (void);

typedef struct
{
const char *name;
Func *func;
} MapEntry;

int function_0 (void)
{
}

int function_1 (void)
{
}

int main (void)
{
MapEntry table [] = {{"f0", function_0}, {"f1", function_1}};

/* call a function */
printf ("%d\n", table [1].func());

return 0;
}

Code untested (uncompiled in fact)


There is no easy way to load the function pointers from a file. C just
doesn't that way. You might want to look at more dynamic langues.
Python, Perl, Scheme etc.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top