Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multiple expressions in a SWITCH STATEMENT

Status
Not open for further replies.

jimineep

Technical User
May 16, 2006
20
0
0
GB
I am trying to do a switch statement which analyses multiple expression:

Code:
int main(){

        char str1[] = "H"; 
        char str2[] = "B"; 



        switch ( * str1, * str2 ){


        case 'H''B':
                printf("%s","YUP");
                break;

        case 'B''B':
                printf("%s","NOPE");
                break;

        }

        printf("%s", str1 );

}

Is this possible? I am getting errors compiling:

Code:
JAMESP>cc test2

        case 'H''B':
................^
%CC-E-NOCOLON, Missing ":".
at line number 21 in file $1$DGA101:[JAMESP.SMP.26856]TEST2.C;17

        case 'B''B':
................^
%CC-E-NOCOLON, Missing ":".
at line number 25 in file $1$DGA101:[JAMESP.SMP.26856]TEST2.C;17

But its not throwing me out for passing 2 expressions to the switch statement...which leads me to believe this may well be possible....

Cheers,
 
> But its not throwing me out for passing 2 expressions to the switch statement.
That's just the comma operator.
The value you're actually switching on is *str2

The rest is not possible using normal cases.


--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
You could set up Macros to pass to the switch which combine the two values into one.

e.g.
Code:
/* combine two bytes into one uint16 for compare */
#define EVENT(a,b) ((uint16)(a<<8 & b))
then call the switch with the macro
Code:
switch(EVENT(*str1,*str2))
{
case EVENT('B','H,):
....

This we use for our software which uses 6 bits to store a resource and 10 bits a feature in that resource, stored over two bytes not consecutive in a message between components.
With enums setup for the resource IDs and features.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top