473,218 Members | 1,795 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,218 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 1481
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: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

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.