471,585 Members | 1,234 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,585 software developers and data experts.

String Pointer To Function

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.
Nov 24 '05 #1
5 1418
On Thu, 24 Nov 2005 05:15:01 GMT, Brian C <br****@si.n-o.rr.sP.aM.com>
wrote in comp.lang.c:

[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

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 24 '05 #2
Jack Klein wrote:
On Thu, 24 Nov 2005 05:15:01 GMT, Brian C <br****@si.n-o.rr.sP.aM.com>
wrote in comp.lang.c:

[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.
Nov 24 '05 #3
Brian C wrote:
Jack Klein wrote:
On Thu, 24 Nov 2005 05:15:01 GMT, Brian C <br****@si.n-o.rr.sP.aM.com>
wrote in comp.lang.c:

[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.
Nov 26 '05 #4
Brian C wrote:
Brian C wrote:
Jack Klein wrote:
On Thu, 24 Nov 2005 05:15:01 GMT, Brian C <br****@si.n-o.rr.sP.aM.com>
wrote in comp.lang.c:

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.)

--
Eric Sosman
es*****@acm-dot-org.invalid
Nov 26 '05 #5
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.
Nov 27 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by al | last post: by
4 posts views Thread by songkv | last post: by
4 posts views Thread by Simon Schaap | last post: by
9 posts views Thread by copx | last post: by
18 posts views Thread by intercom5 | last post: by
23 posts views Thread by Nascimento | last post: by
13 posts views Thread by dimitris67 | last post: by
232 posts views Thread by robert maas, see http://tinyurl.com/uh3t | last post: by
6 posts views Thread by Andrea | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.