简体   繁体   English

如何在EN中将ENUM作为函数参数传递

[英]How to pass ENUM as function argument in C

I have an enum declared as; 我有一个enum声明为;

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 谢谢

That's pretty much exactly how 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)的值。

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的事情

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

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