[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 226
  • Last Modified:

Using a switch statement and array together ?


Hello,

If I had a switch statement set-up as follows:

switch(value)
   {
case 1:      
      do something                                                
break;

case 2:      
      do something                                                
break;

etc….

What would be the correct syntax to replace “case 1” (which checks for the int value of 1) and instead get it check the position 0 in an array (called test) and compare it the value that exist there?

Would it be something like this?

switch(value)
   {
case test[0]:      
      do something                                                
break;
0
andyw27
Asked:
andyw27
1 Solution
 
sunnycoderCommented:
Nope ... case expression has to be a constant ....
Use nested if else for your objective
0
 
RichieHindleCommented:
'case' values have to be constants.  The simplest answer would be to change it to use if / else if / else if, like this:

if (value == test[0])
{
    do something
}
else if (value == test[1])
{
    do something
}
etc...

If you have lots of constants and only one variable (test[0]) then combine the two:

if (value == test[0])
{
    do something
}
else switch (value)
{
case 2:
    do something
    break;

case 3:
    do something
    break;
}
0
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi andyw27,

C needs to know the value of each case statement at compile time.  If you're familiar with function tables, the analogy should be pretty clear.  If the values in the switch statement are close together, C builds a table and merely branches into the table according to the index (the value in the case statement), just as you'd execute a function from a function table based on the index into the table.

Good Luck!
Kent
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
grg99Commented:
Sounds like you want to look up a number in a table.  If there are very few cases you could use if() statement, otherwise there's no getting around searching the table with a loop:

i = 0;   Found = -1;

while( Found < 0 && i < MaxEntries ) do {
         if( TargetValue == Table[ i ] )  Found = i;
         i++;
  }
0
 
PaulCaswellCommented:
The alternative that works here is a function table or a lookup table.

1. Function table (like Kent's suggestion):

void handle0 (void);

(*(*x[])(void)) =
{
 handle0,
 handle1,
 ...
}

void handle0 (void)
{
 // Handles the zero case.
}

// Call it:
...
x[i]();
...

2. Lookup table (like Greg's suggestion above):
struct
{
 int val;
 (*(*x)(void));
} [] =
{
 { 0, handle0 },
 {1, handle1 },
}

Then search the array for an entry with the correct value and call the associated function.

The first is easier if the possible values are close to consecutive. The second takes a little longer but works even if there are long gaps between possible values.

Paul
0
 
andyw27Author Commented:

Thanks for all the reponses, it's filled one of the holes that exist in my knowledge of this subject.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now