String Pointer To Function

B

Brian C

Hello all,
I have a text based program that uses curses for it's interface. I
originally made this program becase my bosses are mostly mainframe
COBOL/CICS people, and are not familiar with UNIX.
It, however, expanded beyond how I envisioned it at the get-go (go
figure). My original idea was just start/stop processes, and run various
scripts, displaying the output, etc. As things got added, it became
clear that it was a maintenance nightmare. Why?

1) They requested it be put onto other servers that run software other
than mine.
2) We moved some processes onto other accounts, therefore, one account
can't start/stop another, etc.

Basically, I needed to maintain seperate config files for the
program telling it which processes it could start/stop, then you add in
the mix the test/production figure. Some config file lines went over 2k,
which my bosses didn't want my SysAdmin to change the limit, fine. Btw,
I used a Windows INI type text file as the config, i.e.

[ProcessName]
Blah1=abc
Halb2=123

So, basically, I overestimated my current project on purpose. It did
require a new function in the program (a shared memory table editor) for
this new server (I have other shared memory table editors already).
I've made this new program still use the Windows INI file model, but
included "ranges" (i.e. ListRange=26 .... List1=A's, List2=B's,
List26=Z's, etc). I've also put on each menu Allow & Block for user ID's
& hostname's, so they don't display if you dont have access. This all
allows one config file for test or production with multiple boxes
considered.
So, long story short, I'm wondering if there is a way, since I do
not recall there is, that you can take a char[] and call it is as a
function. Yes, I know you can have pointers to functions, but if I have:

char String[]="MyFunction";

Is it possible to reference string to call what it contains (i.e.
MyFunction() ?)

Thanks.
 
J

Jack Klein

[snip]
So, long story short, I'm wondering if there is a way, since I do
not recall there is, that you can take a char[] and call it is as a
function. Yes, I know you can have pointers to functions, but if I have:

char String[]="MyFunction";

Is it possible to reference string to call what it contains (i.e.
MyFunction() ?)

It's a FAQ.

http://www.eskimo.com/~scs/C-faq/q20.6.html
 
B

Brian C

Brian said:
Jack said:
[snip]

So, long story short, I'm wondering if there is a way, since I do
not recall there is, that you can take a char[] and call it is as a
function. Yes, I know you can have pointers to functions, but if I have:

char String[]="MyFunction";

Is it possible to reference string to call what it contains (i.e.
MyFunction() ?)



It's a FAQ.

http://www.eskimo.com/~scs/C-faq/q20.6.html
Figures, I should've rtfm. Thanks.
After looking at the sample, it isn't exactly what I was thinking. What
I am looking for (only to do it, no particular reason) is to not have
the hard code a pointer to the function.
For example, if on the menu, the config file says to run "MyFunction",
I just want to take that char[] and call that function. I'm not sure if
it is possible as it wouldn't be resolved at compile-time tho.
 
E

Eric Sosman

Brian said:
Brian said:
Jack said:
So, long story short, I'm wondering if there is a way, since I do
not recall there is, that you can take a char[] and call it is as a
function. Yes, I know you can have pointers to functions, but if I
have:

char String[]="MyFunction";

Is it possible to reference string to call what it contains (i.e.
MyFunction() ?)

It's a FAQ.

http://www.eskimo.com/~scs/C-faq/q20.6.html
Figures, I should've rtfm. Thanks.

After looking at the sample, it isn't exactly what I was thinking.
What I am looking for (only to do it, no particular reason) is to not
have the hard code a pointer to the function.
For example, if on the menu, the config file says to run
"MyFunction", I just want to take that char[] and call that function.
I'm not sure if it is possible as it wouldn't be resolved at
compile-time tho.

You cannot call a function that isn't part of the
program. C lacks the ability to add code to a program
while the program is running. (Pay no attention to
people who say "But if I use dlopen() ..." Machinery
of that sort is not part of the C language, just as
recvfrom() and sigwait() are not part of the language.
If you want to use dlopen() or something like it, ask
your question on a newsgroup devoted to your system.)

Also, compile-time names are not part of a C run-
time program. The names of variables disappear in the
sense that there's no way to convert a string containing
a variable name into a reference to the variable. The
same is true of functions: their names are a compile-time
convenience, not a run-time entity. (Pay no attention to
people who say "But my debugger ..." Debuggers are not
part of the C language, and they rely on extra-linguistic
mechanisms not generally accessible to the programmer who
writes in the language.)
 
B

Brian C

Eric Sosman wrote:
[snip]
You cannot call a function that isn't part of the
program. C lacks the ability to add code to a program
while the program is running. (Pay no attention to
people who say "But if I use dlopen() ..." Machinery
of that sort is not part of the C language, just as
recvfrom() and sigwait() are not part of the language.
If you want to use dlopen() or something like it, ask
your question on a newsgroup devoted to your system.)

Well, dlopen() wasn't something I was considering anyway. I sort of
figured C lacked the ability as I would've seen it done by now, but I
was wondering since this opportunity presented itself to me, and thought
it would be cool (no real need to do it) to do it.
Also, compile-time names are not part of a C run-
time program. The names of variables disappear in the
sense that there's no way to convert a string containing
a variable name into a reference to the variable. The
same is true of functions: their names are a compile-time
convenience, not a run-time entity. (Pay no attention to
people who say "But my debugger ..." Debuggers are not
part of the C language, and they rely on extra-linguistic
mechanisms not generally accessible to the programmer who
writes in the language.)
Yes, I'm well aware of what compilers do as I wrote one myself many
moons ago (not as robust as C, but for a custom language called TPL).
Now that I think of it from the perspective of a compiled program, it
would be impossible, since, as you say the names aren't referenced by
their "source code name".
I guess I'll just do:

if(!strcmp(MenuType,"ScrollList"))
ScrollListMenu(Options);
else if(...)
etc

Thanks.
 

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

Forum statistics

Threads
473,995
Messages
2,570,233
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top