variable names in strings

Is it possible is use a variable name located in an array of characters as the variable itself, rather than the literal meaning of the contents of the string?


I know that I can use something like if(strcmp(VariableNameString, "MyVariable", 20) == 0) MyVariable++; , but is there a way to do this without knowing the exact name of the MyVariable? Thanks in advance.


Comments



  • : I know that I can use something like if(strcmp(VariableNameString, "MyVariable", 20) == 0) MyVariable++; , but is there a way to do this without knowing the exact name of the MyVariable? Thanks in advance.


    No, there is no way inherent in the language. The variable name, unquoted, is just a placeholder to differentiate it from a different variable. By the time your executable is produced, the name of the variable has been turned into memory addresses and offsets because the name doesn't really mean anything.


    You've no choice but to do comparisons like you have in your example. You can do _some_ automation, say by creating a table of strings and relating them to memory addresses and data types or something excruciatingly weird like that. Only you know how complex you need to get, though. A bunch of if (strcmp...) else statements are probably your simplest bet.






  • : You've no choice but to do comparisons like you have in your example. You can do _some_ automation, say by creating a table of strings and relating them to memory addresses and data types or something excruciatingly weird like that. Only you know how complex you need to get, though. A bunch of if (strcmp...) else statements are probably your simplest bet.




    Oh well, it won't be pretty code, but I guess I will resort to a bunch of if(strcmp...) else statements. Thanks again for your help.





  • : Is it possible is use a variable name located in an array of characters as the variable itself, rather than the literal meaning of the contents of the string?

    : I know that I can use something like if(strcmp(VariableNameString, "MyVariable", 20) == 0) MyVariable++; , but is there a way to do this without knowing the exact name of the MyVariable? Thanks in advance.

    I don't know how you want to use this variable,

    but how about having an algorith that calculates

    a unique number for every string (like:

    "string" = 123, "myString" = 456), storing those

    numbers in one array, and then going through a for

    loop (maybe even have another array of pointers to

    the first array). This would mean that you have to know

    all the possible names for your variables though.


Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories