GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 21-Jun-2004, 08:10
mirizar mirizar is offline
New Member
 
Join Date: Jun 2004
Posts: 20
mirizar is on a distinguished road

Get value for variable name defined as string


Hi!

I would like to know how to access the value of a variable whose name is defined as a string. I'm using C.

Ex.
char* varname[] = {"test", "dog"}
test = 5;
dog = 8;

I want to get the value of the variable with varname[1]. So I want to get 5.

How can I do this?

Thanks, Michelle
  #2  
Old 21-Jun-2004, 08:53
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Quote:
Originally Posted by mirizar
Hi!

I would like to know how to access the value of a variable whose name is defined as a string. I'm using C.

Ex.
char* varname[] = {"test", "dog"}
test = 5;
dog = 8;

I want to get the value of the variable with varname[1]. So I want to get 5.

How can I do this?

Thanks, Michelle

Hey Michelle, welcome to GIDForums.

Someone else may have a better solution, but I have never found a better way to do this. I simply would make a small structure that contains both your varname & your variable:

CPP / C++ / C Code:
typedef struct{
  char* varname;
  int value;
}variable;

You could even class this, so that most of the internals were hidden to the calling program.

Like I said, there may be some slick way to do this that I have never come across though.
  #3  
Old 21-Jun-2004, 17:46
mithunjacob's Avatar
mithunjacob mithunjacob is offline
Junior Member
 
Join Date: Jun 2004
Location: Vellore, India
Posts: 65
mithunjacob will become famous soon enough
Just curious, mirizar, but what use do you have of it? Btw, I don't think it's possible and dsmith's idea is probably the closest thing you have to a solution...unless someone finds a way to track down the variable once it's loaded into memory!
__________________
[b]There are times when the Phantom walks the streets as an ordinary man...this is one of those times.
  #4  
Old 21-Jun-2004, 18:36
sho sho is offline
Junior Member
 
Join Date: Jun 2004
Posts: 49
sho will become famous soon enough
Quote:
Originally Posted by mirizar
Hi!

I would like to know how to access the value of a variable whose name is defined as a string. I'm using C.

Ex.
char* varname[] = {"test", "dog"}
test = 5;
dog = 8;

I want to get the value of the variable with varname[1]. So I want to get 5.

How can I do this?

Thanks, Michelle

Hi michelle, what you are trying to do as i understand from your post is that you are trying, or thinking, that with the statement char* varname[] = {"test", "dog"} you put or declare variables with those strings as names... that is like a question i post recently about if it was possible to create a variable with the name of a string... and i think thats not possible

but one solution to this would be what dsmith's suggest, creating a class or a structure with one element that contains the name and other the value of the variable, that way you look for the name you want and change the value of the variable of the structure that has that name...

or more simple, use predefined names for those variables that can identify them and that way you can track them easily...
 
 

Recent GIDBlogWriting a book by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Read a .html file, check that file for links salemite C Programming Language 10 17-Jan-2008 08:56
Re: Conversion: Binary, Decimal, Hexadecimal WaltP C Programming Language 1 10-May-2006 07:49
Including Maps and strings?? maddie C++ Forum 17 05-Jul-2004 07:25
storing a token pointer as a string CoreLEx C Programming Language 1 07-Oct-2003 12:33

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 01:42.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.