By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,260 Members | 1,305 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,260 IT Pros & Developers. It's quick & easy.

Access by string name

P: n/a
jz
Hi,

I'd like to know whether it's possible to access a variable
by its name of which is stored in another string variable.
For example:

int myInt=10;
char *myName="myInt";

Is it possible to access myInt only through myName?

Thanks.
Mar 8 '07 #1
Share this Question
Share on Google+
15 Replies


P: n/a
jz <je*******@comcast.netwrote:
I'd like to know whether it's possible to access a variable
by its name of which is stored in another string variable.
For example:

int myInt=10;
char *myName="myInt";

Is it possible to access myInt only through myName?
Not sure, but I think no. As far as I know, after compiling (linking),
the names are gone. But I think you could implement something like
that by yourself, should not be that hard..
Flo
Mar 8 '07 #2

P: n/a
On Mar 8, 12:47 pm, jz <jessec...@comcast.netwrote:
Hi,

I'd like to know whether it's possible to access a variable
by its name of which is stored in another string variable.
For example:

int myInt=10;
char *myName="myInt";
May be I need to better understand your question. ?
Are you asking how compiler (internally) tracking occurence of each
Variable or something like that ?
>
Is it possible to access myInt only through myName?

Thanks.
--Raxit

Mar 8 '07 #3

P: n/a
I think Java have done the job by reflection, so choose that language
will save a lot time.
On 3月8日, 下午4时12分, Florian Weingarten <f...@go..ccwrote:
jz <jessec...@comcast.netwrote:
I'd like to know whether it's possible to access a variable
by its name of which is stored in another string variable.
For example:
int myInt=10;
char *myName="myInt";
Is it possible to access myInt only through myName?

Not sure, but I think no. As far as I know, after compiling (linking),
the names are gone. But I think you could implement something like
that by yourself, should not be that hard..

Flo

Mar 8 '07 #4

P: n/a
jz
May be I need to better understand your question. ?
Are you asking how compiler (internally) tracking occurence of each
Variable or something like that ?
--Raxit
Just want to know is there a way to do that in C?
Mar 8 '07 #5

P: n/a
Sheth Raxit <ra************@yahoo.co.inwrote:
> int myInt=10;
char *myName="myInt";
May be I need to better understand your question. ?
Are you asking how compiler (internally) tracking occurence of each
Variable or something like that ?
I think he is asking if he can do something like that:

$ perl -e '$someName="hello\n"; $var="someName"; print ${$var};'
hello

in C.
Flo
Mar 8 '07 #6

P: n/a
jz said:
Hi,

I'd like to know whether it's possible to access a variable
by its name of which is stored in another string variable.
For example:

int myInt=10;
char *myName="myInt";

Is it possible to access myInt only through myName?
In practice, you can tie them together in a struct and then store
objects of that kind in some kind of searchable container data
structure that is keyed by name.

(Actually, no matter how you do this, someone will come up with a way
around it, but this should prevent most "accidental" accesses by some
route other than the string.)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 8 '07 #7

P: n/a
Richard Heathfield wrote:
jz said:
Hi,

I'd like to know whether it's possible to access a variable
by its name of which is stored in another string variable.
For example:

int myInt=10;
char *myName="myInt";

Is it possible to access myInt only through myName?

In practice, you can tie them together in a struct and then store
objects of that kind in some kind of searchable container data
structure that is keyed by name.

(Actually, no matter how you do this, someone will come up with a way
around it, but this should prevent most "accidental" accesses by some
route other than the string.)
Is there a practical use for this, inside a program, not when
interfacing with other systems?

Mar 8 '07 #8

P: n/a
santosh said:
Richard Heathfield wrote:
>jz said:
Hi,

I'd like to know whether it's possible to access a variable
by its name of which is stored in another string variable.
For example:

int myInt=10;
char *myName="myInt";

Is it possible to access myInt only through myName?

In practice, you can tie them together in a struct and then store
objects of that kind in some kind of searchable container data
structure that is keyed by name.

(Actually, no matter how you do this, someone will come up with a way
around it, but this should prevent most "accidental" accesses by some
route other than the string.)

Is there a practical use for this, inside a program, not when
interfacing with other systems?
What I have described is simply the concept of containing arbitrary data
that can be sorted and searched via a key. If your key happens to be a
string, then what I described is what you've got. If your key happens
to be "myInt", then it's what the OP wants.

So - yes, there are certainly very practical uses indeed for binary
search trees, hash tables, and so on.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 8 '07 #9

P: n/a
santosh wrote, On 08/03/07 11:29:
Richard Heathfield wrote:
>jz said:
>>Hi,

I'd like to know whether it's possible to access a variable
by its name of which is stored in another string variable.
For example:

int myInt=10;
char *myName="myInt";

Is it possible to access myInt only through myName?
In practice, you can tie them together in a struct and then store
objects of that kind in some kind of searchable container data
structure that is keyed by name.

(Actually, no matter how you do this, someone will come up with a way
around it, but this should prevent most "accidental" accesses by some
route other than the string.)

Is there a practical use for this, inside a program, not when
interfacing with other systems?
Yes. Next question? What, you wanted to know what the practical use was?
Embedding a scripting language within your program which, with
appropriate security measures, can be extremely useful. Look up cint for
one interesting (to me) example.
-
Flash Gordon
Mar 8 '07 #10

P: n/a

"jz" <je*******@comcast.netwrote in message
news:G-******************************@comcast.com...
>
>May be I need to better understand your question. ?
Are you asking how compiler (internally) tracking occurence of each
Variable or something like that ?
--Raxit

Just want to know is there a way to do that in C?
/* module1.c */
#include <stdlib.h>
#include <limits.h>
#include <string.h>

static int myInt1 = 10;
static int myInt2 = 20;
static int myInt3 = 30;

static struct names
{
char *name;
int *addr;
} n[] =
{
{"myInt1", &myInt1},
{"myInt2", &myInt2},
{"myInt3", &myInt3},
};

/* if variable name not found, returns INT_MAX */
int var(const char *name)
{
size_t i = 0;
size_t count = sizeof n / sizeof *n;
for(; i < count; ++i)
if(strcmp(name, n[i].name) == 0)
return *n[i].addr;

return INT_MAX;
}
/* module2.c */

#include <stdio.h>

int var(const char *);

int main()
{
printf("myInt1 == %d\n", var("myInt1"));
printf("myInt2 == %d\n", var("myInt2"));
printf("myInt3 == %d\n", var("myInt3"));
printf("myInt4 == %d\n", var("myInt4"));
return 0;
}
-Mike
Mar 8 '07 #11

P: n/a
On Wed, 07 Mar 2007 23:47:06 -0800, in comp.lang.c , jz
<je*******@comcast.netwrote:
>Hi,

I'd like to know whether it's possible to access a variable
by its name of which is stored in another string variable.
For example:

int myInt=10;
char *myName="myInt";

Is it possible to access myInt only through myName?
Simple answer: No. Once your programme is compiled, all names are
removed so there would be nothing for the runtime environment to
reference "myInt" to. .

Complex answer: yes, using a messy set of structs to point to the
actual data. Its nasty.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Mar 8 '07 #12

P: n/a
On Thu, 08 Mar 2007 18:20:35 GMT, "Mike Wahler"
<mk******@mkwahler.netwrote:
>
"jz" <je*******@comcast.netwrote in message
news:G-******************************@comcast.com...
>>
>>May be I need to better understand your question. ?
Are you asking how compiler (internally) tracking occurence of each
Variable or something like that ?
--Raxit

Just want to know is there a way to do that in C?

/* module1.c */
#include <stdlib.h>
Is there any reason to include <stdlib.h>? My "-pedantic" option says
no :^)
>#include <limits.h>
#include <string.h>

static int myInt1 = 10;
static int myInt2 = 20;
static int myInt3 = 30;

static struct names
{
char *name;
int *addr;
} n[] =
{
{"myInt1", &myInt1},
{"myInt2", &myInt2},
{"myInt3", &myInt3},
};

/* if variable name not found, returns INT_MAX */
int var(const char *name)
{
size_t i = 0;
size_t count = sizeof n / sizeof *n;
for(; i < count; ++i)
if(strcmp(name, n[i].name) == 0)
return *n[i].addr;

return INT_MAX;
}
--
jay
Mar 9 '07 #13

P: n/a

"jaysome" <ja*****@hotmail.comwrote in message
news:3u********************************@4ax.com...
On Thu, 08 Mar 2007 18:20:35 GMT, "Mike Wahler"
<mk******@mkwahler.netwrote:
>>
"jz" <je*******@comcast.netwrote in message
news:G-******************************@comcast.com...
>>>
May be I need to better understand your question. ?
Are you asking how compiler (internally) tracking occurence of each
Variable or something like that ?
--Raxit
Just want to know is there a way to do that in C?

/* module1.c */
#include <stdlib.h>

Is there any reason to include <stdlib.h>? My "-pedantic" option says
no :^)
I used it for the declaration of 'size_t'. Perhaps <string.h>
already declares it, I don't remember. I *know* <stdlib.h>
does, so there it is. :-)

-Mike
Mar 9 '07 #14

P: n/a
"Mike Wahler" <mk******@mkwahler.netwrites:
"jaysome" <ja*****@hotmail.comwrote in message
news:3u********************************@4ax.com...
>Is there any reason to include <stdlib.h>? My "-pedantic" option says
no :^)

I used it for the declaration of 'size_t'. Perhaps <string.h>
already declares it, I don't remember. I *know* <stdlib.h>
does, so there it is. :-)
If you just need size_t, then <stddef.his the minimal header
file that declares it. (But <string.hdoes declare size_t
also.)
--
"I should killfile you where you stand, worthless human." --Kaz
Mar 9 '07 #15

P: n/a
Mike Wahler wrote:
"jaysome" <ja*****@hotmail.comwrote in message
.... snip ...
>
>Is there any reason to include <stdlib.h>? My "-pedantic" option
says no :^)

I used it for the declaration of 'size_t'. Perhaps <string.h>
already declares it, I don't remember. I *know* <stdlib.h>
does, so there it is. :-)
size_t is declared in any or all of stdio.h, stddef.h, string.h and
stdlib.h. See N869 or N1024.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Mar 9 '07 #16

This discussion thread is closed

Replies have been disabled for this discussion.