| | |
Variable Variable Names
![]() |
•
•
Join Date: Jan 2005
Posts: 6
Reputation:
Solved Threads: 0
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
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 >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.
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.
I'm here to prove you wrong.
•
•
Join Date: Jan 2005
Posts: 6
Reputation:
Solved Threads: 0
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
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 >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.
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.
I'm here to prove you wrong.
•
•
Join Date: Jan 2005
Posts: 6
Reputation:
Solved Threads: 0
•
•
•
•
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.
JimH •
•
Join Date: Jan 2005
Posts: 6
Reputation:
Solved Threads: 0
•
•
•
•
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.
JimH •
•
Join Date: Jan 2005
Posts: 6
Reputation:
Solved Threads: 0
•
•
•
•
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.
JimH ![]() |
Similar Threads
Other Threads in the C Forum
- Previous Thread: binary file manipulations
- Next Thread: Interesting problem : please see this and reply
| Thread Tools | Search this Thread |
#include * adobe ansi api array asterisks binarysearch centimeter changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc csyntax database directory dynamic execv feet fgets file fork function getlasterror getlogicaldrivestrin givemetehcodez global grade gtkgcurlcompiling gtkwinlinux hacking hardware highest histogram ide include incrementoperators infiniteloop input interest kernel keyboard kilometer license linked linkedlist linux linuxsegmentationfault list locate logical_drives looping loopinsideloop. lowest match matrix meter microsoft motherboard mqqueue number odf opendocumentformat opensource owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research reversing segmentationfault sequential single socket socketprograming standard string systemcall threads turboc unix user voidmain() wab whythiscodecausesegmentationfault windows.h windowsapi






