C - Convert string to variable name

16,856

Solution 1

This is simply not possible in C.
Check out this question for more details. How to return a variable name and assign the value of the variable returned in c

To quote Wichert, 'I suggest that you reconsider the problem you are trying to solve and check if there might not be a better method to approach it. Perhaps using an array, map or hash table might be an alternative approach that works for you.'

Solution 2

In C you cannot create variable or symbol names dynamically.

Solution 3

Here's one way.

if ( strcmp(str, "str2") == 0 )
{
   // Use m.str2
}

That will be a problem with hundreds of variables. You'll have to come up with some other mechanism in that case.

Solution 4

I will suggest a slightly simpler, albeit maybe not as efficient solution.

This was a solution I came up with for a project after consulting with one of my professors.

Essentially, strings are just ASCII characters, and C file containing variable names can be thought of in the same way.

Thus, suppose you have a list of strings that you would like to turn into variable names for integers.

First, define your structure in a header file that can be accessed by all files in your directory, for instance 'struct.h'.

The first step is to convert the string names to their respective integers

Simply create an empty header, called variable_names.h, include struct.h, and once and for all invoke the following loop in your main file:

const char *strings[] = {"str1", "str2", ... }
fp = fopen("variable_names.h", "a");
fprintf(fp, "#ifndef FILE1_H \n");
fprintf(fp, "#define FILE1_H \n");
fprintf(fp, "extern int* m_integers = {");
int i;
for(i = 0; i < sizeof(strings) - 1; i++){ fprintf(fp, "m.%s,", strings[i]);}
fprintf(fp, "m.%s } ", strings[i+1])
fprintf(fp, "#endif");

Now you have a linear mapping between the string name and value in your structure via the m_integers array. Next is to create some mapping that takes in the string name and points it to this integer. I will use UTHASH, but there are certainly other ways.

Thus, in your main file,

#include "uthash.h"
#include "variable_names.h"
...

struct opcode_table{
     char* opcode_key;
     int opcodes_val;
     UT_hash_handle hh;
 };
struct opcode_table *mapping = NULL;

struct opcode_table* s = NULL;
for (int i = 0; i  < opcode_size; i++){
  s = (struct opcode_table*) malloc(sizeof(*s)); 
  s->opcodes_key = strings[i]; // the string 
  s->opcode_val = m_integers[i]; // the integer
  HASH_ADD(hh,mapping, opcodes_key, sizeof(int),s);
}

^ Please go easy on the code, just a rough example of what could be done. I'm sure there are some mistakes, but high level I believe this should work.

As on overview, the idea is essentially, you wrote to an external file the ascii characters "m.string1", which once is written, is interpreted as an integer via the structure definition, yielding you your desired outcome. Now, you must simply look up the string in the hash table, and you get the integer in the structure.

Also, I would appreciate any feedback if someone finds a better way or this approach is flawed. Thanks!

Share:
16,856
user2511788
Author by

user2511788

Updated on June 05, 2022

Comments

  • user2511788
    user2511788 almost 2 years

    How can I convert a string into a variable name? E.g. I have a list of strings:

         "str1", "str2", "str3", etc.
    

    And a structure:

    struct my_struct {  
           int str1;  
           int str2;  
           int str3;  
    } m = {5, 10, 15, ... etc};  
    

    Given a string "str2", I want to print the variable associated with that name m.str2. Does C have any way to do this?

    Thank you!