串联在C语言的变量名? [英] Concatenating Variable Names in C?

查看:192
本文介绍了串联在C语言的变量名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能在C串联变量名?具体来说,我有一个结构包含在其中6类似的变量名为 1级的Class2 3类

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.

我想通过一个for循环每个变量赋值运行,但我看不出如何做到这一点而不for循环计数器的值以某种方式连接起来的变量名。

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.

我还能怎么做呢?

推荐答案

当你发现自己添加一个整数后缀的变量名,认为我应该用一个阵列

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;
}

注意: C ++编译器将在本BARF因为。你需要找出一个不同的名称,该字段,如果你打算编译这个code和C ++。

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++.

这篇关于串联在C语言的变量名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆