William
William

Reputation: 31

C programming: Using a variable inside another variable name (not sure of the technical term)

I don't know what the technical term is but I would like to do following:

int var_a;
int var_b;
int var_c;
char letter;

letter = 'b';
printf("%d", var_'letter');

However, I don't want to use if statements. Can I directly sub in the rest of the variable name with the variable char so that the computer would see "var_b"?

Upvotes: 1

Views: 2447

Answers (1)

Michael Albers
Michael Albers

Reputation: 3779

Not possible. You could try using an array and indexing it like this:

vars[letter - 'a'];

Where vars[0] would correspond to your var_a and so on.

Upvotes: 6

Related Questions