Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why no switch on pointers?

For instance:

#include <stdio.h>  void why_cant_we_switch_him(void *ptr) {     switch (ptr) {         case NULL:             printf("NULL!\n");             break;         default:             printf("%p!\n", ptr);             break;     } }  int main(void) {     void *foo = "toast";     why_cant_we_switch_him(foo);     return 0; }  gcc test.c -o test test.c: In function 'why_cant_we_switch_him': test.c:5: error: switch quantity not an integer test.c:6: error: pointers are not permitted as case values 

Just curious. Is this a technical limitation?

EDIT

People seem to think there is only one constant pointer expression. Is that is really true, though? For instance, here is a common paradigm in Objective-C (it is really only C aside from NSString, id and nil, which are merely a pointers, so it is still relevant — I just wanted to point out that there is, in fact, a common use for it, despite this being only a technical question):

#include <stdio.h> #include <Foundation/Foundation.h>  static NSString * const kMyConstantObject = @"Foo";  void why_cant_we_switch_him(id ptr) {     switch (ptr) {         case kMyConstantObject: // (Note that we are comparing pointers, not string values.)             printf("We found him!\n");             break;         case nil:             printf("He appears to be nil (or NULL, whichever you prefer).\n");             break;         default:             printf("%p!\n", ptr);             break;     } }  int main(void) {     NSString *foo = @"toast";     why_cant_we_switch_him(foo);     foo = kMyConstantObject;     why_cant_we_switch_him(foo);      return 0; }  gcc test.c -o test -framework Foundation test.c: In function 'why_cant_we_switch_him': test.c:5: error: switch quantity not an integer test.c:6: error: pointers are not permitted as case values 

It appears that the reason is that switch only allows integral values (as the compiler warning said). So I suppose a better question would be to ask why this is the case? (though it is probably too late now.)

like image 964
Michael Avatar asked Feb 22 '10 01:02

Michael


2 Answers

Because there is only one constant pointer expression

Given that only a single constant pointer expression exists, the switch statement has little to offer pointer expressions. You have cited essentially the only possible construction.

like image 55
DigitalRoss Avatar answered Sep 25 '22 16:09

DigitalRoss


A switch compares the variable with a set of compile-time constants. Other than null, I can't see any valid compile time constants that you might compare a pointer with. For example:

switch (ptr) {     case &var1: printf ("Pointing to var1"); break;    case &var2: printf ("Pointing to var2"); break; } 

var1 and var2 are likely different in each run of the program, and would not be compile time constants. One possibility might be that they are addresses of memory-mapped ports that are always fixed, but otherwise I don't see how you could easily expand this from your two cases (null / not-null).

like image 44
Tarydon Avatar answered Sep 21 '22 16:09

Tarydon