How to pass ENUM as function argument in C
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.
精彩评论