473,320 Members | 1,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 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 1486
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: al | last post by:
char s = "This string literal"; or char *s= "This string literal"; Both define a string literal. Both suppose to be read-only and not to be modified according to Standard. And both have...
4
by: songkv | last post by:
Hi, I am trying to reassign an array of char to a string literal by calling a function. In the function I use pointer-to-pointer since I want to reassign the "string array pointer" to the string...
4
by: Simon Schaap | last post by:
Hello, I have encountered a strange problem and I hope you can help me to understand it. What I want to do is to pass an array of chars to a function that will split it up (on every location where...
9
by: copx | last post by:
I've noticed that it's valid to pass a string literal (like "test") to a function that expects a const char *. Does this mean that in C a string literal is automatically converted to a const char *...
18
by: intercom5 | last post by:
I'm writing a program in C, and thus have to use C strings. The problem that I am having is I don't know how to reallocate the space for a C string outside the scope of that string. For example:...
23
by: Nascimento | last post by:
Hello, How to I do to return a string as a result of a function. I wrote the following function: char prt_tralha(int num) { int i; char tralha;
13
by: dimitris67 | last post by:
How can I replace an occurence of p(a string) in an other string(s) with np(new string).. char* replace _pattern(char *s,char *p,char *np) PLEASE HELP ME!!!!!
19
by: Paul | last post by:
hi, there, for example, char *mystr="##this is##a examp#le"; I want to replace all the "##" in mystr with "****". How can I do this? I checked all the string functions in C, but did not...
232
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first...
6
by: Andrea | last post by:
Hi, suppose that I have a string that is an hexadecimal number, in order to print this string I have to do: void print_hex(unsigned char *bs, unsigned int n){ int i; for (i=0;i<n;i++){...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.