C语言 如何在C中将ENUM作为函数参数传递
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4654655/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pass ENUM as function argument in C
提问by user437777
I have an enum declared as;
我有一个枚举声明为;
typedef enum
{
NORMAL = 0,
EXTENDED
}CyclicPrefixType_t;
CyclicPrefixType_t cpType;
I need a function that takes this as an argument :
我需要一个将其作为参数的函数:
fun (CyclicPrefixType_t cpType) ;
func declaration is :
func 声明是:
void fun(CyclicPrefixType_t cpType);
Please help. I don't think it is correct.
请帮忙。我不认为这是正确的。
Thanks
谢谢
回答by paxdiablo
That's pretty much exactlyhow you do it:
这几乎是正是你怎么做:
#include <stdio.h>
typedef enum {
NORMAL = 31414,
EXTENDED
} CyclicPrefixType_t;
void func (CyclicPrefixType_t x) {
printf ("%d\n", x);
}
int main (void) {
CyclicPrefixType_t cpType = EXTENDED;
func (cpType);
return 0;
}
This outputs the value of EXTENDED(31415 in this case) as expected.
这会EXTENDED按预期输出值(在本例中为 31415)。
回答by rogerdpack
The following also works, FWIW (which confuses slightly...)
以下也有效,FWIW(有点令人困惑......)
#include <stdio.h>
enum CyclicPrefixType_t {
NORMAL = 31414,
EXTENDED
};
void func (enum CyclicPrefixType_t x) {
printf ("%d\n", x);
}
int main (void) {
enum CyclicPrefixType_t cpType = EXTENDED;
func (cpType);
return 0;
}
Apparently it's a legacy C thing.
显然这是一个遗留的 C 东西。

