[Update]
[Update ++] I will probably accept one of the answers below. However, I forgot to point out that our listings are non-contiguous and have a wide range, which can make a difference.
Mezhtubki and this site are awash with questions requiring a text from the listing.
I cannot find a canonical way to do this (and would take it as an answer to this question), so let's see if we can cross each other between us.
In our code, we have several arrays of structures containing pairs of enumerations and corresponding strings.
The problem is that the strings have different lengths, so we code a search function for each of them, which for loops over an array of structures, trying to match the enumeration and return the corresponding text if a match is found.
Let's take the following two far-fetched examples:
typedef enum
{
north,
south,
east,
west
} E_directions;
struct direction_datum
{
E_directions direction;
char direction_name[6];
};
struct direction_datum direction_data[] =
{
{north, "north"},
{south, "south"},
{east, "east"},
{west, "west"},
};
typedef enum
{
hearts,
spades,
diamonds,
clubs,
} E_suits;
struct suit_datum
{
E_suits suit;
char suit_name[9];
};
struct suit_datum suit_data[] =
{
{hearts, "hearts"},
{spades, "spades"},
{diamonds, "diamonds",},
{clubs, "clubs"},
};
In addition to the length of the string, they are the same / identical, so theoretically we should be able to encode the generalized function for the loop through direction_dataor suit_data, taking into account the index and return of the corresponding text.
, - , ( zer0, , , ).
?
char *Get_text_from_enum(int enum_value,
void *array,
unsigned int array_length,
unsigned int size_of_array_entry)
{
unsigned int i;
unsigned int offset;
for (i = 0; i < array_length; i++)
{
offset = i * size_of_array_entry;
if ((int) * ((int *) (array+ offset)) == enum_value)
return (char *) (array + offset + sizeof(int));
}
return NULL;
}
printf("Expect south, got %s\n",
Get_text_from_enum(south,
direction_data,
ARRAY_LENGTH(direction_data),
sizeof(direction_data[0])));
printf("Expect diamonds, got %s\n",
Get_text_from_enum(diamonds,
suit_data,
ARRAY_LENGTH(suit_data),
sizeof(suit_data[0])));