473,379 Members | 1,185 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,379 software developers and data experts.

Access by string name

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
15 1687
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
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
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
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
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
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
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
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
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

"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
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
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

"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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much...
7
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I...
11
by: Grasshopper | last post by:
Hi, I am automating Access reports to PDF using PDF Writer 6.0. I've created a DTS package to run the reports and schedule a job to run this DTS package. If I PC Anywhere into the server on...
42
by: lauren quantrell | last post by:
So many postings on not to use the treeview control, but nothing recently. Is it safe to swim there yet with Access 2000-Access 2003?
2
by: Dean Slindee | last post by:
Anybody written code in VB.NET to: 1) show a print preview window of reports already written and stored in an Access 2002 database; or 2) execute the print of a report stored in an Access 2002...
1
by: gm | last post by:
Hi; I have written a database that tracks all the installation we have ever done. I have a small heating company. I have recently started keeping a directory of digital photographs of the...
9
by: Wayne Smith | last post by:
I've come up against a major headache that I can't seem to find a solution for but I'm sure there must be a workaround and I would really be grateful of any help. I'm currently building a web...
1
by: cpajoe2001 | last post by:
I am having an issue and after searching around online for a day and half now and finding others with the same problem but yet no solution to my issue I am looking for help. What i have is ServerA...
1
by: Chris | last post by:
Hi, I'm using the following code DataTable tables = ((OleDbConnection)oleconn).GetOleDbSchemaTable( OleDbSchemaGuid.Tables, new object { null, null, null, "TABLE" }); foreach (DataRow r in...
1
by: gihan | last post by:
Hi, I have a problem accessing remote webservice from my asp code. Instead of returning results, it returns list of web methods it has. Wonder where i'm doing wrong. Also note that, this is a...
1
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: 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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.