944,191 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 11066
  • C RSS
Jan 11th, 2005
0

Variable Variable Names

Expand Post »
I need to assign a value to a variable named in a sting variable. Like this ...

int temperature;
int pressure;
int volume;

int variable_value;
char Inbuffer[2048];

printf("%s","Variable List: temperature, pressure, volume");
printf ("%s","Input variable name from list above:");
gets(Inbuffer);
ScanError=scanf("%s\n",variable_name);
....
printf("%s","Input new value for variable:");
gets(inbuffer);
ScanError=scanf("%d", variable_value);
....

// This is what I want to do . . .

// Find a simple way to convert a string representation of a variable name
// to a variable name and assign a value to it.

// For instance, if variable_name=volume and variable_value=250;
// How do I get volume=250 using the string variable_name when
// variable_name is 'volume'?


Would appreciate any help!
JimH
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
JimH is offline Offline
6 posts
since Jan 2005
Jan 12th, 2005
0

Re: Variable Variable Names

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.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Jan 12th, 2005
0

Re: Variable Variable Names

Or since you only have three possabilities, could it be as simple as

if (stricmp( variable_name, "temperature" ) == 0)
<set temp>
else if (stricmp( variable_name, "pressure" ) == 0)
<set pressure>
etc

?
Reputation Points: 36
Solved Threads: 11
Posting Pro in Training
Chainsaw is offline Offline
436 posts
since Jun 2004
Jan 12th, 2005
0

Re: Variable Variable Names

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jan 13th, 2005
0

Re: Variable Variable Names

Appreciate the suggestions and advice.

I am using C for a program having about 60 user assignable system variables. Using a hash table was my first impulse, but, would like to keep additional coding and variable space to a minimal. Is there any way to use a string variabe as a variable name? However, I'll use your best advice. I haven't ventured into C++ yet, which might make things easier.

By the way, are C console programs difficult to migrate to C++?

Thanx
JimH
Reputation Points: 10
Solved Threads: 0
Newbie Poster
JimH is offline Offline
6 posts
since Jan 2005
Jan 13th, 2005
0

Re: Variable Variable Names

You might investigate the std::map template, where you could do something like:

map<string, string> variableMap;

and then fill it in with commands like:

variableMap["CustomerName"] = "Chainsaw";

once you understand maps and iterators, the look isn't too bad.
Reputation Points: 36
Solved Threads: 11
Posting Pro in Training
Chainsaw is offline Offline
436 posts
since Jun 2004
Jan 13th, 2005
0

Re: Variable Variable Names

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jan 15th, 2005
0

Re: Variable Variable Names

Quote originally posted by jwenting ...
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.
Thanx for suggestion. Gives me something to work on.

JimH
Reputation Points: 10
Solved Threads: 0
Newbie Poster
JimH is offline Offline
6 posts
since Jan 2005
Jan 15th, 2005
0

Re: Variable Variable Names

Quote originally posted by Chainsaw ...
You might investigate the std::map template, where you could do something like:

map<string, string> variableMap;

and then fill it in with commands like:

variableMap["CustomerName"] = "Chainsaw";

once you understand maps and iterators, the look isn't too bad.
This is good input for me. Thanx.

JimH
Reputation Points: 10
Solved Threads: 0
Newbie Poster
JimH is offline Offline
6 posts
since Jan 2005
Jan 15th, 2005
0

Re: Variable Variable Names

Quote originally posted by Narue ...
>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.
A lot of good food for thought. Thanx

JimH
Reputation Points: 10
Solved Threads: 0
Newbie Poster
JimH is offline Offline
6 posts
since Jan 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: binary file manipulations
Next Thread in C Forum Timeline: Interesting problem : please see this and reply





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC