I just wonder how can I switch on the string "B1" ? Don't ask why this string :-)
If you mean, how to write something similar to this:
switch (string) {
case "B1":
// do something
break;
/* more case "xxx" parts */
}
Then the canonical solution in C is to use an if-else ladder:
if (strcmp(string, "B1") == 0)
{
// do something
}
else if (strcmp(string "xxx") == 0)
{
// do something else
}
/* more else if clauses */
else /* default: */
{
}
Hi Niklas
To add to Phimueme's answer above, if your string is always two characters, then you can build a 16-bit int out of the two 8-bit characters - and switch on that (to avoid nested switch/case statements).
Mike
If you have many cases and do not want to write a ton of strcmp()
calls, you could do something like:
switch(my_hash_function(the_string)) {
case HASH_B1: ...
/* ...etc... */
}
You just have to make sure your hash function has no collisions inside the set of possible values for the string.
I think the best way to do this is separate the 'recognition' from the functionality:
struct stringcase { char* string; void (*func)(void); };
void funcB1();
void funcAzA();
stringcase cases [] =
{ { "B1", funcB1 }
, { "AzA", funcAzA }
};
void myswitch( char* token ) {
for( stringcases* pCase = cases
; pCase != cases + sizeof( cases ) / sizeof( cases[0] )
; pCase++ )
{
if( 0 == strcmp( pCase->string, token ) ) {
(*pCase->func)();
break;
}
}
}
There is no way to do this in C. There are a lot of different approaches. Typically the simplest is to define a set of constants that represent your strings and do a look up by string on to get the constant:
#define BADKEY -1
#define A1 1
#define A2 2
#define B1 3
#define B2 4
typedef struct { char *key; int val; } t_symstruct;
static t_symstruct lookuptable[] = {
{ "A1", A1 }, { "A2", A2 }, { "B1", B1 }, { "B2", B2 }
};
#define NKEYS (sizeof(lookuptable)/sizeof(t_symstruct))
int keyfromstring(char *key)
{
int i;
for (i=0; i < NKEYS; i++) {
t_symstruct *sym = lookuptable + i;
if (strcmp(sym->key, key) == 0)
return sym->val;
}
return BADKEY;
}
/* ... */
switch (keyfromstring(somestring)) {
case A1: /* ... */ break;
case A2: /* ... */ break;
case B1: /* ... */ break;
case B2: /* ... */ break;
case BADKEY: /* handle failed lookup */
}
There are, of course, more efficient ways to do this. If you keep your keys sorted, you can use a binary search. You could use a hashtable too. These things change your performance at the expense of maintenance.
Assuming little endianness and sizeof(char) == 1, you could do that (something like this was suggested by MikeBrom).
char* txt = "B1";
int tst = *(int*)txt;
if ((tst & 0x00FFFFFF) == '1B')
printf("B1!\n");
It could be generalized for BE case.
This is how you do it. No, not really.
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <arpa/inet.h>
main (void)
{
uint32_t s[0x40];
assert((unsigned char)1 == (unsigned char)(257));
memset(s, 0, sizeof(s));
fgets((char*)s, sizeof(s), stdin);
switch (ntohl(s[0])) {
case 'open':
case 'read':
case 'seek':
puts("ok");
break;
case 'rm\n\0':
puts("not authorized");
break;
default:
puts("unrecognized command");
}
return 0;
}