[英]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.