![]() |
|
#1
|
|||
|
|||
Get value for variable name defined as stringHi!
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
|
||||
|
||||
|
Quote:
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:
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
|
||||
|
||||
|
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
|
|||
|
|||
|
Quote:
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 GIDBlog
Writing a book by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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