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 ab[26] = { ALFA, BRAVO, CHARLIE, ... }
At that point you will be able to pass "ab" as you would any other pointer.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.