Access an enum using an index in C

后端 未结 4 1505
梦毁少年i
梦毁少年i 2021-01-13 09:17

Consider:

enum Test
{
    a = 3,
    b = 7,
    c = 1
};

I want to access the enum using an index. Something like this:

for         


        
4条回答
  •  难免孤独
    2021-01-13 09:27

    As someone else mentioned, this is not the purpose of an enum. In order to do what you are asking, you can simply use an array:

    #define a 3
    #define b 7
    #define c 1
    
    int array[3] = { a, b, c };
    int i;
    
    for( i = 0; i < sizeof(array)/sizeof(array[0]); i++ ) {
        doSomething( array[i] );
    }
    

提交回复
热议问题