tags:

views:

472

answers:

5

Is it possible to concatenate variable names in C? Specifically, I have a struct that contains 6 similar variables in it called class1, class2, class3, etc.

I want to run through a for loop to assign each variable a value, but I can't see how to do it without somehow concatenating the variable name with the value of the for loop counter.

How else could I do this?

+34  A: 

When you find yourself adding an integer suffix to variable names, think I should have used an array.

struct mystruct {
    int class[6];
};

int main(void) {
    struct mystruct s;
    int i;
    for (i = 0; i < 6; ++i) {
        s.class[i] = 1000 + i;
    }

    return 0;
}

Note: A C++ compiler will barf at this because of class. You will need to figure out a different name for that field if you plan to compile this code as C++.

Sinan Ünür
+1, would add more if I could
Donnie
+1 Seems obvious to experienced programmers but this seems to be a common pitfall for beginners.
Charlie Salts
do not use the word class as a field name!
glebm
Doh! Can't believe I didn't think of this. Thanks!
Tyler
@Glex: This is C, not C++. I don't think a downvote is needed.
Sinan Ünür
sure, but now you've written code that is valid C but not valid C++, something that is rather rare.
Jason S
@Glex: In C, `class` is not a special word in any way.
sth
@Jason S: Really? How about `char *p = malloc(100)`? C is not C++ and vice versa. See also http://en.wikipedia.org/wiki/Compatibility_of_C_and_C%2B%2B#Constructs_valid_in_C_but_not_C.2B.2B
Sinan Ünür
+5  A: 

There are dynamic languages where you can do this sort of thing - C is not one of these languages. I agree with Sinan - arrays or STL vectors are the way to go.

As a thought experiment - what would happen if you have 100,000 of these variables? Would you have 100,000 lines of code to initialise them?

Charlie Salts
+1 Perl, for example, allows you to use such symbolic references, but their use is **strongly** discouraged. http://perl.plover.com/varvarname.html
Sinan Ünür
+1  A: 

The C preprocessor can concatenate symbols, but have you considered just using an array?

amrox
Preprocessor concatenation won't work, because if you concatenate `class` with the loop variable `i` you'll produce `classi` and the compiler will complain that no such symbol exists.
benzado
A: 

What you could also do, is write an implementation of a hash map. Since the set of keys (that would be like variable names) of the hash map does not change over time, for each hash map you could keep an array of its keys for iterating efficiently. But that would be a total (crazy) overkill, especially in C ;)

Pretty much anything is possible in C, it's a great language to learn :)

glebm
A: 

perhaps the CERT-C secure coding rule PRE05-C 'Understand macro replacement when concatenating tokens or performing stringification' could help you. For deep details have a look at this link: https://www.securecoding.cert.org/confluence/display/seccode/PRE05-C.+Understand+macro+replacement+when+concatenating+tokens+or+performing+stringification.

For short, define first a macro JOIN_AGAIN(x,y) (x##y) and then JOIN(x,y) JOIN_AGAIN(x,y) The JOIN_AGAIN macro allows to expand the value of the loop couner which will be concatenated to the var name.

Cheers Pierre Bui

Pierre Bui
Wrong. You'll merely produce `classi`; preprocessor simply produces more characters, and has nothing to do with run-time values.
GMan
you're right GMAN, thanks for this comment. oups, not well awake at sunday morning.sorry to post a wrong answer.Pierre Bui
Pierre Bui