[英]What does this Method declaration/definition mean? (something to do with passing an array?)
[英]What is mean by passing the enum 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;
}
输出:
BLUE BLACK WHITE
你不能。 我的意思是,如果你有的话
typedef enum {
ALFA,
BRAVO,
CHARLIE,
DELTA
} Alphabet;
你无法将这些信息传递给一个函数。
你可以做的是,声明一个整数数组并为该数组赋值 Alphabet的值:
字母ab [26] = {ALFA,BRAVO,CHARLIE,...}
此时,您将能够像任何其他指针一样传递“ab”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.