I am new in C programming language and have an (if statement) and need to transfer it into switch statement.
My problem is that I have a field named (node_kind) of char* type and I compare it’s content in the if statement using (strcmp) but I don’t know how to do that in the switch statement.
Would you plz tell me how ?
Here is a short quote of my program
if (strcmp(node->node_kind, "VAR_TOKEN_e") == 0)
job = visitor->visitjob_VAR_TOKEN;
if (strcmp(node->node_kind, "INT_e") == 0)
job = visitor->visitjob_int;
if (strcmp(node->node_kind, "BOOL_e") == 0)
job = visitor->visitjob_bool;
You can use gperf (website) to generate a perfect hash, which turns strings into integers. You’ll have something like this:
In your header file:
In your gperf file:
In your switch statement: