2015-09-04 82 views
-9

我有一些整數變量,我將它們命名爲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

我想用循環做到這一點,如果有可能的話 –

回答

6

做正確的方法是聲明一個整數數組,而不是10個不同的變量:

int n[10]; 

現在,你可以通過n[9]訪問與n[0] 10個INT變量。

+0

但這次我不允許使用數組 –

+7

@MuhammadHaryadiFutra爲什麼不能?這是一個愚蠢的廢話要求,就像「不用手指寫這個程序,只用鼻子打字」。 – Lundin

+0

@Lundin:聽起來更像是一種愚蠢的面試問題,「不使用分號寫一個hello world程序」。 –

-2
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

8

答案很簡單:聲明一個數組,而不是作爲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

0

它幾乎沒有不可能訪問您的變量,就像你想要的,沒有任何數組。

所有這一切在我腦海中,是要考慮10個不同的情況下,針對每個變量,於是:

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

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

既然你不能用「真實」的陣列,可以使用一些動態內存,而不是(真的很傻但...):

#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... */ 
    } 
    ... 
} 

如果你真的有,你要訪問像你說的,以及保存指針數組把它們(或類似以上)和訪問他們的方式幾個變量,但它的仍然沒有名字。如果你必須訪問他們的名字,我想你留下了預處理器。我不知道有任何其他適當的方式來做到這一點。