Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch-case with pointers [duplicate]

Tags:

c

gcc

Possible Duplicate:
Why no switch on pointers?

int main(int argc, char** argv)
{
  void* not_a_pointer = 42;
  switch(not_a_pointer)
  {
    case 42:
      break;
  }

  return 0;
}

.

error: switch quantity not an integer

How can I portably use a switch-case for the value of a variable with a pointer type? The reason for this is that one of the callback functions in an API I'm using has a void* argument.

like image 835
kaykun Avatar asked Mar 14 '26 10:03

kaykun


1 Answers

try casting to intptr_t, which is an integer type:

switch((intptr_t)not_a_pointer)

etc...

like image 161
Nathan Fellman Avatar answered Mar 17 '26 01:03

Nathan Fellman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!