Quote:
Originally Posted by mluckham
main()
{
char funcname[] = "foo";
...
switch (funcname)
{
case "foo":
foo(a,b);
case "bar":
bar(a,b);
}
...
}
}
|
Oi... that's going to lead you into a world of trouble.
The C language does not allow for direct comparison of a string, because a string isn't a type, it's a null-terminated array of characters. "foo" is of type
const char *, which is an address in your program's memory.
funcname is also of type char *, so switch is going to implicitly compare the addresses of the two "strings" and it will never work, because it cannot compare the data at the addresses.
You need to do the following:
Code:
if(strcmp(funcname, "foo") == 0) {
foo(a, b);
} else if(strcmp(funcname, "bar")) {
bar(a, b);
}
Switch only works with strings in PHP and maybe Javascript, as far as I know. You're right though, function pointers are as close to what he's looking for as you can get.