make an hashmap of pointers to the variables with the name of the variable as the key.
You can then simply look up the pointer by the name of the variable, dereference it, and you have your variable back.
jwenting
duckman
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
>I need to assign a value to a variable named in a sting variable.
Why? Most of the time this problem can be solved differently by looking at it the right way. Since this appears to be C, it takes quite a bit of work to solve it properly, and that would be a waste of time if you really want something simpler but can't see it.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>would like to keep additional coding and variable space to a minimal.
To do a lookup table using string keys properly, you're looking at either a hash table or a binary search tree. Those are the simplest data structures that give you good performance. If your goal is minimal extra work then the hash table is your best bet, likewise for variable space. Memory-wise you're looking at 60 string-value pairs at the very least if you want everything in high speed memory at run-time. If you don't care about ideal performance then an array of about 60 structures won't bog you down too much, even with a naive sequential search.
>Is there any way to use a string variabe as a variable name?
No, not without a lot of framework in C. In C++ you can use existing libraries to do what you would otherwise have to code manually (assuming no third party stuff).
>By the way, are C console programs difficult to migrate to C++?
It depends on the C program and the desired level of C++-correctness. For well written ISO C programs, only a few minor changes will be needed to compile as C++ (if any are needed at all). If you want proper C++ and not just C code compiled as C++ then it takes more work.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401