开发者

How to pass ENUM as function argument in C

开发者 https://www.devze.com 2023-02-03 19:10 出处:网络
I have an enum declared as; typedef enum { NORMAL = 0, EXTENDED }Cycl开发者_StackOverflowicPrefixType_t;

I have an enum declared as;

typedef enum 
{
   NORMAL = 0,           
   EXTENDED              

}Cycl开发者_StackOverflowicPrefixType_t;

CyclicPrefixType_t cpType;  

I need a function that takes this as an argument :

fun (CyclicPrefixType_t cpType) ;  

func declaration is :

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.


The following also works, FWIW (which confuses slightly...)

#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.

0

精彩评论

暂无评论...
验证码 换一张
取 消