0

In C, how do you use the value of a variable or a field within a struct as the name of a variable or field within a struct to be used in a program?

i.e.:

char variable_name[];

struct_x.value_of_variable_name = 1;
// assuming the variable struct_x.value_of_variable_name is an int
1
  • 1
    This is rarely a good idea even in languages that support it. Commented Mar 3, 2012 at 21:21

1 Answer 1

4

You can't. Variable names exist only at compile-time.

You could emulate this behaviour manually by creating a lookup table (that maps a string to a pointer, for instance), but there is no language support for this.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.