Martin Denion
Martin Denion

Reputation: 382

How to make a lookup table with uint8_t associated to a string?

I would like to make a lookup table with uint8_t associated to a string.

How do you think I can do this?

Here is my code:

static const uint16_t ReadAccess[][2] =
{
        /* Value in b3b2 in byte 1, read access description */
        {0x0,           "ALWAYS"},
        {0x1,              "RFU"},
        {0x2,      "PROPRIETARY"},
        {0x3,              "RFU"}
};

Upvotes: 0

Views: 311

Answers (1)

MikeCAT
MikeCAT

Reputation: 75062

You can use structures to group members with multiple types to create one new type.

struct table_element {
        uint8_t intValue;
        const char* strValue;
};

static const struct table_element ReadAccess[] = {
        {0x0,           "ALWAYS"},
        {0x1,              "RFU"},
        {0x2,      "PROPRIETARY"},
        {0x3,              "RFU"}
};

Upvotes: 1

Related Questions