1

I have an enumeration in C as follows:

enum menu
{
    PASTA,
    PIZZA,
    DIET_COKE,
    MOJITO,
};

Since I haven't explicitly mentioned the integer values corresponding to these elements, they are assigned values 0,1,2, and 3 respectively.

Say I decide to add another 100 or so items to my enum. Then is there a way to access these elements by the number they are associated with? (Like how we can access an element of an array using its index)

Prerk
  • 55
  • 9
  • 1
    Do you mean "access as a string containing its name?" The question doesn't otherwise make any sense. – Weather Vane May 21 '22 at 08:34
  • I meant, say I wanted to access the fourth element, is there a way I can refer to is as menu(3)? – Prerk May 21 '22 at 08:39
  • @Prerk but why would you want to do this? What problem dou you think you can solve with this? – Jabberwocky May 21 '22 at 08:40
  • Yes, access as a string containing its name, – Prerk May 21 '22 at 08:40
  • @Prerk there are no strings here. – Jabberwocky May 21 '22 at 08:41
  • Possibly related C++ question: [How to Get enum item name from its value](https://stackoverflow.com/questions/11714325/how-to-get-enum-item-name-from-its-value) – Weather Vane May 21 '22 at 08:41
  • @Prerk do you expect `menu(3)` to result in `"MOJITO"`, so `printf("%s", menu(3))` would print `MOJITO`? – Jabberwocky May 21 '22 at 08:42
  • @Jabberwocky well, I'm new to C and I want to have a list of strings, without using a 2D array. Now I thought I could just list everything I wanted to in an enum and then refer to it using the number associated with it. If you have a better suggestion then do let me know since I'm not well-versed in C and open to any other suggestions too. – Prerk May 21 '22 at 08:43
  • @Jabberwocky yes that's what i'm going for. – Prerk May 21 '22 at 08:44
  • 1
    There are some tricky solutions such as the one I linked, but what I do is to use the text editor: first I set up the array of string literal names (a 1D array of pointers), and then I use copy/paste and find/replace to make the `enum` defintion, which is probably just as quick to set up anyway, on the rare occasions I need it. – Weather Vane May 21 '22 at 08:45
  • @Prerk you can't. Once the program is compiled the identifiers like `MOJITO` are gone, just like variable names are gone. You need to write a conversion function yourself. It's about 5 + N lines of code, where N is the number of items in your menu. – Jabberwocky May 21 '22 at 08:46
  • @WeatherVane Oh yes it sounds like a feasible solution. I'll give it a shot. Thank you so much! – Prerk May 21 '22 at 08:48
  • @Jabberwocky that makes a lot of sense. Thank you so much for the clarification! – Prerk May 21 '22 at 08:48
  • Use a 1D array of string literal pointers, not a 2D array. – Weather Vane May 21 '22 at 08:49
  • @WeatherVane a 1D array of string literal pointers is simple and efficient, but it's error prone because if you have a lot of strings and you dont pay attention, the string literal pointers might get out of alignement with the enums – Jabberwocky May 21 '22 at 08:51
  • 1
    @Jabberwocky everything in C is error-prone... but I take your point. It's easier to maintain if you have a function with `switch` and `case` statements to return a pointer to a string literal. – Weather Vane May 21 '22 at 09:02

1 Answers1

0

The enums are nothing but named constants in C. Same as you declare a const using

#define PASTA 0
#define PIZZA 1
#define DIET_COKE 2
#define MOJITO 3

for example.

With the enum the compiler does that for you automatically. So there is no way to access enums in a way you want in C, unless you create an array for them.

Update for an example

An example use case to show how do you implement a string list with the accompany of enum as index:

#include <stdio.h>

char *stringsOfMenu[] = {
    "Pasta",
    "Pizza",
    "Diet Coke",
    "Mojito"
};

enum menuIndex {
    PASTA,
    PIZZA,
    DIET_COKE,
    MOJITO
};

int main(void) {
    
    // For example you show menu on the screen first
    puts("Please select");
    puts("-------------");
    for(int i = 0; i <= MOJITO; i++) {
        printf("[%d] - %s\n", i+1, stringsOfMenu[i]);
    }
    putchar('\n');
    printf("Make a choice: ");
    int choice = -1;
    scanf("%d", &choice);
    
    if(choice <= 0 || choice > MOJITO+1) {
        puts("Not a valid choice");
        return 1;
    }
    
    // Note that the choice will contain the counting number.
    // That's why we convert it to the index number.
    printf("Your choice %s is in progress, thank you for your patience!\n", stringsOfMenu[choice-1]);
    return 0;
}

This is an output demo:


Please select
-------------
[1] - Pasta
[2] - Pizza
[3] - Diet Coke
[4] - Mojito

Make a choice: 2
Your choice Pizza is in progress, thank you for your patience!

Kozmotronik
  • 2,080
  • 3
  • 10
  • 25
  • Ah okay, that seems to make sense. Thank you so much! – Prerk May 21 '22 at 08:45
  • Is there a way I can have a list of strings, without using a 2-D array? I was going to try and use enum for that, but I now understand that it is not possible. – Prerk May 21 '22 at 08:46
  • @Prerk define a struct line `struct Foo {enum menu; const char *dishname;}`and set up a a 1D array of `struct Foo`s. – Jabberwocky May 21 '22 at 08:48
  • Let me know what do you want to design exactly to be able to give you better recommendations? – Kozmotronik May 21 '22 at 08:48
  • @Kozmotronik Yes, I'll definitely give it a shot. Seems like that might be the solution I'm looking for. – Prerk May 21 '22 at 08:50
  • @Kozmotronik for my project, I need to write a program that lists several food items in a menu, and lets you choose items, and calculate and display a bill. – Prerk May 21 '22 at 08:51
  • @prek hint: for this you need a struct like `struct Item {enum menu id; float price; const char *name;};`. And `struct Items items[] = {{PASTA, 8.45, "Pasta"}, {PIZZA, 9.50, "Super Pizza"}, {DIET_COKE, 3.30, "Diet Coke"}, ...}`. Don't do this without a `struct` or it will end up in tears. – Jabberwocky May 21 '22 at 08:53
  • You cannot create a list of strings without using 2D array. But with the help of that enum you defined the access for that array would be easier. Let me show you some example. – Kozmotronik May 21 '22 at 08:53
  • @Kozmotronik I think I'll try to centre my project around the idea you've given. Thank you so much for your time and effort! – Prerk May 21 '22 at 09:28
  • @Jabberwocky I will implement using struct for sure then, thank you xD – Prerk May 21 '22 at 09:29