如何通过C中的相关数字访问枚举中的元素?

发布于 2025-01-30 17:45:29 字数 239 浏览 3 评论 0原文

我在C中的枚举如下:

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

由于我没有明确提到与这些元素相对应的整数值,因此分别为它们分别分配了值0,1,2和3。

假设我决定在我的枚举中再添加100个左右的项目。 那么,是否有一种方法可以按照与之关联的数字访问这些元素? (就像我们如何使用其索引访问数组的元素)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

無心 2025-02-06 17:45:29

这些枚举只不过是在C中命名常数

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

使用枚举,编译器会自动为您做到这一点。因此,除非您为它们创建一个数组,否则无法在C中以您想要的方式访问枚举。

更新示例

一个示例用例,以显示如何在enum的随附作为索引的附带来实现字符串列表:

#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;
}

这是一个输出演示:


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!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文