简体   繁体   中英

Is there a way to declare a function argument to take an anonymous enum?

If I have an anonymous enum, is there any way to pass a value of that type to a function? For example,

typedef struct {
    enum { On, Off } status;
    int max_amps;
} SWITCH;

void make_switches(){
    SWITCH switch1 = createSwitch( On, 15 );
    SWITCH switch2 = createSwitch( Off, 20 );
}

SWITCH* createSwitch( ??? status, int max_amps ){
    SWITCH* new_switch = malloc( sizeof( SWITCH ) );
    new_switch->status = status;
    new_switch->max_amps = max_amps;
    return new_switch;
}

I would like to pass the value of the anonymous enum into the createSwitch() function. Is there any way to do this?

As others have suggested, you can simply use an int in the place of ??? .

This is because as per 6.7.2.2/3 of C11 standard (Committee draft):

The identifiers in an enumerator list are declared as constants that have type int and may appear wherever such are permitted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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