36

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);  

The function declaration is:

void fun(CyclicPrefixType_t cpType);

How can I fix it? I don't think it is correct.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user437777
  • 1,423
  • 4
  • 17
  • 28

2 Answers2

48

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.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
15

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.

rogerdpack
  • 62,887
  • 36
  • 269
  • 388
  • 1
    In this example, `CyclicPrefixType_t` is not actuaslly a type but just the name of the `enum` - so it's a bit different. – stdcerr Nov 26 '14 at 19:04