[newbie] intern ??

W

Walter Roberson

Olivier said:
Is there a way to specify a identifier from a string?
String would be "aux1", identifier would be aux1?
I fear that no, but ...

As you feared, the answer is NO.

You would not want to do this without constraints anyhow, as
the user could deliberately choose one of your variables
that are important for controlling the program, or could choose
one of the variables of the implementation. You would effectively
need to check the string against all the variable names that
you want to be allowed -- and if you are going through that trouble
it is little more work to actually code the variable instead of
hoping to be able to "interpret" it.

if (strcmp(varname,"aux1") == 0) readout = aux1
else if (strcmp(varname,"beta2") == 0) readout = beta2

and so on
 
A

Ancient_Hacker

Olivier said:
Dear all,

Is there a way to specify a identifier from a string?
String would be "aux1", identifier would be aux1?
I fear that no, but ...
A.O.

Not in standard C. But there are a few options outside the pale:

(1) Generate a link map. Many compilers spit out the global variables
as named relocatable symbols, often listed in the link map. From the
map you may be able to figure out the absolute address of the variable.

(2) Make your own name to address table, with name and address pairs.

(3) Have the comp[iler generate "debug info". Then pore thru the
STABS records lookin for the identifier. Doable but tricky.

(4) Use RTTI if your compiler has that option.
 
D

David Resnick

Olivier said:
Dear all,

Is there a way to specify a identifier from a string?
String would be "aux1", identifier would be aux1?
I fear that no, but ...
A.O.

I think the question is what you are really trying to do?

This unchecked code fragment may do what you want...

int *theVariable = NULL;
int bar = 0;
int bas = 0;

if (strcmp(identifier, "bar") == 0) {
theVariable = &bar;
}
else if (strcmp(identifier("bas") == 0) {
theVariable = &bas;
}
else {
/* deal with error */
}

Or maybe having some scheme where you register variables by name in
some hash table and can look them up by name and get back a pointer to
the desired variable will satisfy your requirements? Or are you
looking for some built in language facility?

-David
 
O

Olivier

Dear all,

Is there a way to specify a identifier from a string?
String would be "aux1", identifier would be aux1?
I fear that no, but ...
A.O.
 
O

Olivier

Ok, thanks to all---
What I was trying to do was that:
while developping a program, I get more and more
variables, which I regularly want to check, in case
anything gets broke. That's not really debugger work,
just one step above. I can update my table of
on-demand-printable variables as you suggested
(with by the way a fairly easy to update script).
My idea was to ask the special user which variable
s/he wanted to see, recover the name as a string,
say "ItsDog", from it reconstruct the real name
which is Yuri->ItsDog and displayed it via
Write_ItsDog, since all my variables have
fixed routine-name for display.
I'll update my table, that's not too hard!
Thanks again! A.O.
 
D

Dave Thompson

Not in standard C. But there are a few options outside the pale:

(1) Generate a link map. Many compilers spit out the global variables
as named relocatable symbols, often listed in the link map. From the
map you may be able to figure out the absolute address of the variable.
For what I consider to be the usual meaning of 'global', which in C is
more precisely '(symbol) with external linkage', I would say any
implementation that does linking pretty much has to use relocatable
linker symbols for these to obtain the required functionality, and I
know of none that doesn't. (The standard doesn't actually require
separate compilation and linking, or indeed even actual compilation.)

Some, IME most, implementations also have some kind of linker symbols
(often 'local' or 'hidden', else mangled) for C entities with internal
linkage, i.e. declared with 'static'. But there is more variation over
whether these appear in the link map. If any.
(2) Make your own name to address table, with name and address pairs.
This is of course the only completely portable method.
(3) Have the comp[iler generate "debug info". Then pore thru the
STABS records lookin for the identifier. Doable but tricky.
Or on many targets, pore through the linker symbols which are retained
in the linked executable file. Same result as #1.
(4) Use RTTI if your compiler has that option.

I know of no C compiler that has RTTI. C++ compilers do -- but they
provide data including the name of the _type_ not of the variable(s).

- David.Thompson1 at worldnet.att.net
 

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,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top