如何根据循环索引访问任何变量名称

问题描述:

我有一些整数变量,我将它们命名为n0n9。我想用循环访问它们。我试过这样做的代码:如何根据循环索引访问任何变量名称

int n0 = 0, n1 = 0, n2 = 0, n3 = 0, n4 = 0; 
int n5 = 0, n6 = 0, n7 = 0, n8 = 0, n9 = 0;  

for(i = 0; i < 10; i++){ 
    if(digit == 1){ 
     n[i] = n[i] + 1; 
    }    
} 

我知道这是不正确的方式,但我不知道如何正确地做到这一点。

+4

我不明白问题 – StarShine

+0

你不需要一个循环来访问**命名的**变量 – mathematician1975

+0

我想用循环做到这一点,如果有可能的话 –

做正确的方法是声明一个整数数组,而不是10个不同的变量:

int n[10]; 

现在,你可以通过n[9]访问与n[0] 10个INT变量。

+0

但这次我不允许使用数组 –

+7

@MuhammadHaryadiFutra为什么不能?这是一个愚蠢的废话要求,就像“不用手指写这个程序,只用鼻子打字”。 – Lundin

+0

@Lundin:听起来更像是一种愚蠢的面试问题,“不使用分号写一个hello world程序”。 –

int n[10] = {0}; 
/*or you can initilize like this also , this will make all the elements 0 in array*/ 

for(i = 0; i < 10; i++){ 
    if(digit == 1){ 
     n[i] = n[i] + 1; 
    }    
} 

试试这个,让我知道

+0

为什么你不使用初始化列表的任何原因? – Lundin

+0

是的...我知道如何使用初始化列表,我知道如何使用指针也。但问问题的人不知道一件事。 这就是为什么我试图告诉他在他的代码中实际发生错误的地方 – niyant

+0

请不要为此付出代价... – niyant

答案很简单:声明一个数组,而不是作为int n[10]


先进的答案:它似乎并没有在这里是如此,但在你需要使用的数组项个别变量名,不管出于什么原因的情况下,你可以使用一个联盟:

typedef union 
{ 
    struct 
    { 
    int n0; 
    int n1; 
    int n2; 
    ... // and so on 
    int n9; 
    }; 

    int array[10]; 

} my_array_t; 

如果你有一个古老的恐龙编译器,然后用一个变量名声明结构如struct { ... } s;


如何在实际的,现实世界的p使用上述类型rogram:

my_array_t arr = {0}; 

    for(int i=0; i<10; i++) 
    { 
    arr.array[i] = i + 1; 
    } 

    // access array items by name:  
    printf("n0 %d\n", arr.n0); // prints n0 1 
    printf("n1 %d\n", arr.n1); // prints n1 2 

或者,您可以初始化成员的名字:

my_array_t arr = 
    { 
    .n0 = 1, 
    .n1 = 2, 
    ... 
    }; 

傻,如何使用上述类型赋值给变量,而无需使用数组人工例子符号:

my_array_t arr = {0}; 

    // BAD CODE, do not do things like this in the real world: 

    // we can't use int* because that would violate the aliasing rule, therefore: 
    char* dodge_strict_aliasing = (void*)&arr; 

    // ensure no struct padding: 
    static_assert(sizeof(my_array_t) == sizeof(int[10]), "bleh"); 

    for(int i=0; i<10; i++) 
    { 
    *((int*)dodge_strict_aliasing) = i + 1; 
    dodge_strict_aliasing += sizeof(int); 
    } 

    printf("n0 %d\n", arr.n0); // prints n0 1 
    printf("n1 %d\n", arr.n1); // prints n1 2 

    for(int i=0; i<10; i++) 
    { 
    printf("%d ",arr.array[i]); // prints 1 2 3 4 5 6 7 8 9 10 
    } 
+0

评论规则表示为避免表达感谢和+1之类的内容,但幸运的是它没有提及任何内容关于说LOL。 – Jite

它几乎没有不可能访问您的变量,就像你想要的,没有任何数组。

所有这一切在我脑海中,是要考虑10个不同的情况下,针对每个变量,于是:

int i; 
int n0, n2, n3 ... n9; 

for(i=0; i<10; i++) 
{ 
    switch(i) 
    { 
    case 0: n0++; break; 
    case 1: ... 
    } 
} 

既然你不能用“真实”的阵列,可以使用一些动态内存,而不是(真的很傻但...):

#define NUMVALS 10 
int main(int argc, char *argv[]) 
{ 
    int *values, *ptr, i; 
    ptr = values = malloc(sizeof(int) * NUMVALS); 

    for (i = 0; i < NUMVALS; i++) { 
     ptr++ = 0; /* Set the values here or do what you want */ 
        /* Ofc values[i] would work but looks like array access... */ 
    } 
    ... 
} 

如果你真的有,你要访问像你说的,以及保存指针数组把它们(或类似以上)和访问他们的方式几个变量,但它的仍然没有名字。如果你必须访问他们的名字,我想你留下了预处理器。我不知道有任何其他适当的方式来做到这一点。