Right, the case-label needs to be an integer constant that the compiler knows at the compile-time. You are using an indexed array, which the compiler can't make into a constant at the time of compilation.
Vart suggests an enum, which is one good choice, e.g. :
Code:
enum somename {Value1, Value2, Value3};
somename val;
...
switch(val)
{
case Value1:
...
case Value2:
...
case Value3:
...
default:
...
}
If you have an array that contains some values, and you really want to switch on the content of the array, then you may want to use a loop to find the correct index and then switch on the index. Or you could make a two-element array:
Code:
struct st {
int val;
char *str;
};
static const st command_list[] = {
{ 0, "Hello" },
{ 1, "NHL" },
{ 2, "NFL" },
...
};
...
for(i = 0; i < sizeof(command_list) / sizeof(command_list[0]); i++)
{
if (command_list[i].val == command_read)
{
printf("%s\n", command_list[i].str);
break;
}
}
There may be other solutions that are better for what you want to do, but without understanding what you are actually trying to achieve, this is the best suggestions I can give.
--
Mats