44

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.

3
  • 8
    Why do you think that is incorrect? What did your compiler tell you? Commented Jan 11, 2011 at 6:14
  • Looks just fine to me; have you tried compiling it? Commented Jan 11, 2011 at 6:17
  • 4
    Don't use typenames with _t at the end these are usually reserved, in particular by POSIX. But as the others say, your prototype is ok. Commented Jan 11, 2011 at 7:59

2 Answers 2

54

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.

Sign up to request clarification or add additional context in comments.

Comments

18

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.

3 Comments

In this example, CyclicPrefixType_t is not actuaslly a type but just the name of the enum - so it's a bit different.
if I pass a value which is not in the enum list also received by ' void func ()` and the same value is printed. is it valid behavior ?
Maybe if 2 enums have the same value? Wouldn't be common... :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.