Impossible Conversion of Strings to Variables in C
In C , unlike scripting languages such as Ruby and Python, converting strings to variables and vice versa is not feasible. This is due to the language's emphasis on performing as much work as possible at compile time instead of runtime.
Variable Declaration and Initialization
For variables, you can declare them statically, as in:
int count;
Alternatively, you can defer the assignment of their values to runtime, as in:
std::cin >> count;
Collections of Variables
For collections of variables, where the exact number is unknown, you can create vectors or maps, as in:
std::vector<int> counts;
The Significance of Variable Names
In C , variable names serve as references to the variables themselves. Assigning variable names dynamically would add complexity and slow down the program's execution.
Conclusion
Therefore, in C , it is neither possible nor beneficial to convert strings to variables dynamically at runtime. Instead, variables should be declared and initialized according to the language's static typing and compilation-time optimization principles.
The above is the detailed content of Why Can't I Convert Strings to Variables in C ?. For more information, please follow other related articles on the PHP Chinese website!