简体   繁体   English

通过枚举数组是什么意思?

[英]What is mean by passing the enum array?

can we pass enum as an array or pointer. 我们可以将枚举作为数组或指针传递。 I heard this type of question somewhere in internet. 我在互联网上的某个地方听到过这种问题。 so I want to check what is that mean. 所以我想检查一下这是什么意思。 how can we do that? 我们怎么能这样做? example? 例?

Just like any other array: 就像任何其他数组一样:

#include <stdio.h>

enum colour {
    WHITE,
    RED,
    ORANGE,
    YELLOW,
    GREEN,
    BLUE,
    INDIGO,
    VIOLET,
    BLACK
};

char *colour_names[] = {
    "WHITE",
    "RED",
    "ORANGE",
    "YELLOW",
    "GREEN",
    "BLUE",
    "INDIGO",
    "VIOLET",
    "BLACK"
};

void show_colours(enum colour colours[], int count) {
    int i;

    for (i = 0; i < count; ++i) {
        printf("%s ", colour_names[(int)colours[i]]);
    }
    printf("\n");
}

int main(int argc, char **argv) {
    enum colour estonia[] = {BLUE, BLACK, WHITE};
    show_colours(estonia, 3);
    return 0;
}

Output: 输出:

BLUE BLACK WHITE 

You can't. 你不能。 I mean, if you have 我的意思是,如果你有的话

typedef enum {
    ALFA,
    BRAVO,
    CHARLIE,
    DELTA
} Alphabet;

there's no way you can pass that information, as is, to a function. 你无法将这些信息传递给一个函数。

What you CAN do is, to declare an array of integers and assign to that array the values of Alphabet: 你可以做的是,声明一个整数数组并为该数组赋值 Alphabet的值:

Alphabet ab[26] = { ALFA, BRAVO, CHARLIE, ... } 字母ab [26] = {ALFA,BRAVO,CHARLIE,...}

At that point you will be able to pass "ab" as you would any other pointer. 此时,您将能够像任何其他指针一样传递“ab”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM