不知何故,我的 switch 语句没有经过我的任何案例,但它不应该合二为一吗?(我使用https://stackoverflow.com/a/4014981/960086作为参考)。
没有输出,之后应用程序被阻塞。
#include <stdio.h>
#include <stdlib.h>
#define BADKEY -1
#define string1 1
#define string2 2
#define string3 3
#define string4 4
char *x = "string1";
typedef struct {char *key; int val; } t_symstruct;
static t_symstruct lookuptable[] = {
{ "string1", string1 }, { "string2", string2 }, { "string3", string3 }, { "string4", string4 }
};
#define NKEYS (sizeof(lookuptable)/sizeof(t_symstruct))
int keyfromstring(char *key) {
int i;
for (i=0; i < NKEYS; i++) {
t_symstruct *sym = lookuptable + i;
printf("before: \n");
if (strcmp(sym->key, key) == 0) { //creates the ERROR
printf("inside: \n");
return sym->val;
}
printf("after: \n");
}
return BADKEY;
}
void newFunction(char *uselessVariable) {
printf("keyfromstring(x): %i \n", keyfromstring(x));
switch(keyfromstring(x)) {
case string1:
printf("string1\n");
break;
case string2:
printf("string2\n");
break;
case string3:
printf("string3\n");
break;
case string4:
printf("string4\n");
break;
case BADKEY:
printf("Case: BADKEY \n");
break;
}
}
int main(int argc, char** argv) {
newFunction(line);
return (EXIT_SUCCESS);
}