eSlavko
eSlavko

Reputation: 450

More elegant enum / const declaration

I wonder if there is better way to declare enum constant with string name. For now I have something like this:

enum ch{
    Ch1,
    Ch2,
    Ch3,
    Ch4
};

const char chNames[][12] = {
    "LivingRoom",
    "Kitchen",
    "Garden",
    "Toilet"
};

result = DoSomeJobWithInptChan(Ch2);
printf("Result from %s is %i\n", &chNames[Ch2][0], result);

The problem is that every string take 12 bytes no matter how long it is. The other problem is that declaring is prone to error (in example is just 4 lines but there are many) as it's simple to skip some line.

So I prefer something to declare both in same line for every enum. Something to enumerate constant and string like that:

enum ch{
    Ch1, "LivingRoom"
    Ch2, "Kitchen"
    Ch3, "Garden"
    Ch4, "Toilet"
};

and if there is no need to declare length in every string even better. Some solution?

p.s. must be in C style. Not C+.

Upvotes: 0

Views: 12

Answers (0)

Related Questions