Home > Net >  How to decalre array of enums?
How to decalre array of enums?

Time:10-29

I have the following enum:

enum {
       ClientRx,
       ClientTx,

       Server1Rx,
       Server1Tx,
       Server1RxDropped,
       Server1TxDropped,

       Server2Rx,
       Server2Tx,
       Server2RxDropped,
       Server2TxDropped,

       Server3Rx,
       Server3Tx,
       Server3RxDropped,
       Server3TxDropped,

       Server4Rx,
       Server4Tx,
       Server4RxDropped,
       Server4TxDropped,

       MaxVal
};

Is there a way to create an array of server enums that will fit in original enum to make this code more generic? (e.g. If I want to extend number of server counters to 100...)

Something in the form:

typedef enum {
           ServerRx,
           ServerTx,
           ServerRxDropped,
           ServerTxDropped
}eServerCounters;

eServerCounters ServerCountersArray[4];
enum {
       ClientRx,
       ClientTx,

       ServerCountersArray,

       MaxVal
};

Get enum value:

ServerCountersArray[2].ServerRx

CodePudding user response:

In C enumerations are a way to create symbolic integer constants in the global namespace.

With your definition

eServerCounters ServerCountersArray[4];

you create an array of four elements. Each element can have any of the values from the eServerCounters enumeration.

For example you can check if the third element is equal to ServerRx like

if (ServerCountersArray[2] == ServerRx) ...

Of perhaps you need to swtich between the different values:

switch (ServerCountersArray[i])
{
case ServerRx:
    // Do something here...
    break;
case ServerTx:
    // Do something here...
    break;

case ServerRxDropped:
    // Do something here...
    break;

case ServerTxDropped:
    // Do something here...
    break;
}

Thinking a little more about the code and the comments by Cheatah, it seems like what you might need is an array of structures:

struct counter
{
    unsigned rx;
    unsigned tx;
    unsigned rx_dropped;
    unsigned tx_dropped;
};

struct counter counters[4];

Now you can access the counter for a specific "channel" from the array, like e.g.

for (unsigned ch = 0; ch < 4;   ch)
{
    printf("Channel %u:\n", ch   1);
    printf("    Rx: %u (dropped %u)\n", counters[ch].rx, counters[ch].rx_dropped);
    printf("    Tx: %u (dropped %u)\n", counters[ch].tx, counters[ch].tx_dropped);
}

CodePudding user response:

You need to generate large enun definition

int main(int argc, char **argv)
{
    int nservers =  -1;

    printf("typedef enum {\n"
           "\t\tClientRx,\n"
           "\t\tClientTx,\n\n");
    if(argc > 1 && sscanf(argv[1], "%d", &nservers) == 1 && nservers != -1)
    {
        for(int server = 1; server <= nservers; server  )
        {
            printf("\t\tServer%dRx,\n" , server);
            printf("\t\tServer%dTx,\n" , server);
            printf("\t\tServer%dRDropped,\n" , server);
            printf("\t\tServer%dTxDropped,\n\n" , server);
        }
    }

    printf("} serverEnumType;\n");
}

https://godbolt.org/z/sTGezaer7

  • Related