• C

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;
andyw27Asked:
Who is Participating?
 
RichieHindleConnect With a Mentor Commented:
'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
 
sunnycoderCommented:
Nope ... case expression has to be a constant ....
Use nested if else for your objective
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
Protect Your Employees from Wi-Fi Threats

As Wi-Fi growth and popularity continues to climb, not everyone understands the risks that come with connecting to public Wi-Fi or even offering Wi-Fi to employees, visitors and guests. Download the resource kit to make sure your safe wherever business takes you!

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.