switch (string) {
case "...": ...
case "...": ...
}
--
Weichao Wang
Institute for Organic Chemistry
Hamburg University
E-mail: ww...@rrz.uni-hamburg.de
wa...@chemie.uni-hamburg.de
Is it impossible to put a string varialbe after switch as follows?
switch (string) {
case "...": ...
case "...": ...
}
Yes.
--
(supporter of the campaign for grumpiness where grumpiness is due in c.l.c)
Please: do not email me copies of your posts to comp.lang.c
do not ask me C questions via email; post them instead
Weichao Wang wrote:
>
> Is it impossible to put a string varialbe after switch as follows?
yes. you must put integral constants for each case.
>
> switch (string) {
> case "...": ...
> case "...": ...
> }
>
one way to solve the problem is:
if(strcmp(string, "first case") == 0)
...
else if(strcmp(string, "second case") == 0)
...
else
...
good luck
--
-----------------------------------------------------------
Luis Grave mailto:lgr...@ccg.uc.pt
Dept. Teletrabalho, Visualisacao and Tecnologias Multimedia
Centro de Computacao Grafica http://www.ccg.uc.pt
-----------------------------------------------------------
: Is it impossible to put a string varialbe after switch as follows?
: switch (string) {
: case "...": ...
: case "...": ...
: }
: Yes.
OTOH, if the strings all differed in their first characters, you
could switch on the first character... I was trying to think of
some peverted method involving multi-byte integer constants, but
couldn't come up with anything suitably shocking.
Ben Pfaff (pfaf...@pilot.msu.edu) wrote:
: fc4...@math.uni-hamburg.de (Weichao Wang) writes:
: Is it impossible to put a string varialbe after switch as follows?
OTOH, if the strings all differed in their first characters, you
could switch on the first character... I was trying to think of
some peverted method involving multi-byte integer constants, but
couldn't come up with anything suitably shocking.
Oh, you mean something like this? I think it's portable among systems
with 8-bit bytes, although I always hesitate saying something like
that on c.l.c since I usually get shot down.
#define tuple(A,B) \
(((A) << 8) + (B))
static void
parse_weekday (char **cp, int *weekday)
{
if (**cp == 0)
date_error ((_("Day of the week expected in date value.")));
switch (tuple (tolower ((unsigned char) ((*cp)[0])),
tolower ((unsigned char) ((*cp)[1]))))
{
case tuple ('s', 'u'):
*weekday = 1;
break;
case tuple ('m', 'o'):
*weekday = 2;
break;
case tuple ('t', 'u'):
*weekday = 3;
break;
case tuple ('w', 'e'):
*weekday = 4;
break;
case tuple ('t', 'h'):
*weekday = 5;
break;
case tuple ('f', 'r'):
*weekday = 6;
break;
case tuple ('s', 'a'):
*weekday = 7;
break;
default:
date_error ((_("Day of the week expected in date value.")));
}
while (isalpha ((unsigned char) **cp))
(*cp)++;
}
#undef tuple
Not to do what you want (act on the characters to which string points).
You can do something like:
do {
if (!strcmp(string,"test1")) {
/* do stuff */
break;
}
if (!strcmp(string,"test2")) {
/* do stuff */
break;
}
/* ... */
/* default action */
} while(0);
--
Martin Ambuhl (mam...@earthlink.net)
Note: mam...@tiac.net will soon be inactive
>Ben Pfaff (pfaf...@pilot.msu.edu) wrote:
>: fc4...@math.uni-hamburg.de (Weichao Wang) writes:
>
>: Is it impossible to put a string varialbe after switch as follows?
>
>: switch (string) {
>: case "...": ...
>: case "...": ...
>: }
>
>: Yes.
>
>OTOH, if the strings all differed in their first characters, you
>could switch on the first character... I was trying to think of
>some peverted method involving multi-byte integer constants, but
>couldn't come up with anything suitably shocking.
Compute the 32-bit CRCs of the strings in which you are interested,
and use those as the case values. The probability of this giving wrong
answers is actually very small.
-- Mat.
>Ben Pfaff (pfaf...@pilot.msu.edu) wrote:
>: fc4...@math.uni-hamburg.de (Weichao Wang) writes:
>
>: Is it impossible to put a string varialbe after switch as follows?
>
>: switch (string) {
>: case "...": ...
>: case "...": ...
>: }
>
>: Yes.
>
>OTOH, if the strings all differed in their first characters, you
>could switch on the first character...
It also depends on whether the input string is guaranteed to match any
of the choices or not.
If these conditions aren't met you can still switch on the first character
and then add extra processing in each case to deal with all of the entries
starting with that letter. It will probably reduce processing time compared
to a long if () else if () chain.
--
-----------------------------------------
Lawrence Kirby | fr...@genesis.demon.co.uk
Wilts, England | 7073...@compuserve.com
-----------------------------------------
String constants are not allowed as a case label.
Other people have suggested the multiple "if/else".
Here is another method: use tables.
For example,
#include <string.h>
int Is_Weekday_Name(const char * const day_name)
{
static const char weekday_names[] =
{"Monday", "Tuesday", "Wednesday", "Thursday",
"Friday", "Saturday", "Sunday", NULL};
unsigned int index = 0;
while (weekday_names[index] != 0)
{
if (strcmp(weekday_names, day_name) == 0)
return 1;
}
return 0;
}
Another method is to use a table of <string, function pointer> vectors.
1. Define a type for the function pointer (makes life easier).
2. Create structure containing string and function.
3. Create an array of the structures.
4. Search the array by comparing strings.
5. If string is found, execute the function via the function pointer.
--
Thomas Matthews
email: mat...@stamps.stortek.com
Weichao Wang wrote:
> Is it impossible to put a string varialbe after switch as follows?
>
> switch (string) {
> case "...": ...
> case "...": ...
> }
Yes, it is not possible.
However, you can create an array of strings and use a loop
of strcmp() to generate an interger which you can use with
switch(). Here is an example:
#include<stdio.h>
#include<string.h>
#include <time.h>
#define SIZEOF(x) sizeof(x)/sizeof(*x)
void doStr(const char *s) {
static char *str[] = {"today","tomorrow","yesterday"};
char day[16];
time_t now;
struct tm *t;
int i;
if((now = time(NULL)) == (time_t)-1) {
puts("time unavailable");
return;
}
for(i = 0;i < SIZEOF(str);i++)
if(strcmp(s,str[i]) == 0) break;
switch(i) {
case 0: t = localtime(&now);
if(strftime(day,16,"%a %d%b%y",t))
printf("Today is %s\n",day);
break;
case 1: t = localtime(&now);
t->tm_hour += 24;
now = mktime(t);
if(strftime(day,16,"%a %d%b%y",localtime(&now)))
printf("Tomorrow will be %s\n",day);
break;
case 2: t = localtime(&now);
t->tm_hour -= 24;
now = mktime(t);
if(strftime(day,16,"%a %d%b%y",localtime(&now)))
printf("Yesterday was %s\n",day);
break;
default: printf("Command \"%s\" unknown\n",s);
}
}
int main(void) {
doStr("today");
doStr("tomorrow");
doStr("yesterday");
doStr("future");
return 0;
}
--
Al Bowers
Tampa, FL
mailto:abo...@combase.com
http:www.gate.net/~abowers/index.html
...
>#define SIZEOF(x) sizeof(x)/sizeof(*x)
Careful, this might work in this particular program but to make it safe
(or at least a lot safer) in general you need to parenthesize it properly
#define SIZEOF(x) (sizeof(x)/sizeof *(x))
>
>void doStr(const char *s) {
> static char *str[] = {"today","tomorrow","yesterday"};
Also consider
static const char *str[] = {"today","tomorrow","yesterday"};
or even
static const char *const str[] = {"today","tomorrow","yesterday"};