Pass enum to function in C?

How can I pass enum to a function not as a parameter?

Pass enum to function in C?
I have for example this

enum Resource { ROCK = 1, PAPER = 2, SCISSORS= 4};

how shall I pass that to a function like

void* player(void* av, enum Resource r) {
....
}

but instead I just want void* av as the only parameter for player

throughout the player function I only used r, but not specifically ROCK, PAPER or SCISSORS

How can I achieve that without needing to pass enum Resource r as a parameter?
closed account (SECMoG1T)
if you don't want to pass it as a parameter then you have to ensure that the variable is in the global scope which is not a very good idea .

EDIT:
throughout the player function I only used r, but not specifically ROCK, PAPER or SCISSORS
if you had passed a param this would be fine but if you want to use global scoping then you'll have to substitute the r with a specific value one that can be evaluated at compile time.
Last edited on
then you'll have to substitute the r with a specific value one that can be evaluated at compile time.

how exactly can I do that?
closed account (SECMoG1T)
you just create a global variable with an enum value...

1
2
3
4
5
6
7
8
9
10
11

enum Resource { ROCK = 1, PAPER = 2, SCISSORS= 4}; 

Resource item=PAPER;///global variable
//or
Resource item=Resource::PAPER/// if you turn to strongly typed enums

void* player(void* av) {
///use the variable here
///....
} 


whereas if you passed the param r the compiler could determine for you the exact value of Resource obj.
Last edited on
so basically r can't be used?
closed account (SECMoG1T)
you could use it if it were a param because the compiler would do the type checking stuff and value deduction for you, if you want to use a global variable instead then having a variable r with no value would result to an undefined behavior.
Last edited on
Topic archived. No new replies allowed.