>Hi Folks,
>
>I hope it's not inappropriate to ask a C language question here.
>
>Is there a more elegant way to do the equivalent of a switch statement for a
>character string value than a chain of if (strcmp())... else if
>(strcmp())... else if.... ?
>
>What I tried to do was
>
> switch (char-var) {
> case "string1":
> ...
> }
>
>Eventually I figured out character strings aren't a data type in C. Sigh...
>
>Thanks.
>Jerry
>(PL/I programmer struggling to learn C while writing code on a deadline
>GAK!)
>
>
>**********************************************************
Here's the way I do it. It's in C++ but the same thing should work in
C just remove the // comments and if your C compiler doesn't like
"const" remove it as well.
static char key[1024];
static const char* tokens[] =
{"tempo", "clef", "key", "timing", "instrument", "voice",
"note", "rest", "amplitude", "double", "sharp", "flat", "neutral",
".", "..", "tie", "measure", NULL};
switch( matchList(key, tokens) ) {
case 0: // ----- tempo #:# -----
...
break;
case 5: // ----- voice -----
...
default:
}
/* ------------------------- matchList ------------------------- */
int matchList(const char *key, const char *L[]) {
int indx;
if(!key)
return -2;
if(!L)
return -3;
for(indx=0; L[indx]; indx++) {
if( !strcmp(key, L[indx]) )
return indx;
}
return -1;
}
--------------------------------------------------------------
Robert E. Anderson email: [EMAIL PROTECTED]
Systems Programmer phone: (603) 862-3489
UNH Research Computing Center fax: (603) 862-1761
--------------------------------------------------------------
**********************************************************
To unsubscribe from this list, send mail to
[EMAIL PROTECTED] with the following text in the
*body* (*not* the subject line) of the letter:
unsubscribe gnhlug
**********************************************************