using (char*) in switch statment

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ahumam0
    New Member
    • Apr 2010
    • 3

    using (char*) in switch statment

    Hello everybody.
    I am new in C programming language and have an (if satament) and need to transfer it into switch statment.
    My problem is that I have a field named (node_kind) of char* type and I compare it's content in the if statment using (strcmp) but I don't know how to do that in the switch statment.
    Would you plz tell me how ?
    Here is a short quote of my program

    Code:
    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;
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    The short answer is that you can't. The switch and case variables are all compared as int's. If you need to compare a type other than int (or that can't be promoted to int) then use if/else if/else.

    If it turns out that either of the arguments to your strcmp's are single-character strings then strcmp is only examining a single char ... and a char can be promoted to an int. However, it does not look like you are using single-character strings.

    Comment

    • johny10151981
      Top Contributor
      • Jan 2010
      • 1059

      #3
      donbock is right in his way.

      But you can convert it to switch statement.
      Simple way would be.
      get a new variable, Integer will be perfect for this.
      and then assign the value of integer under the same condition you have made and then you just need to use your integer number in your switch, so far I think, entire process is pointless

      Comment

      • ahumam0
        New Member
        • Apr 2010
        • 3

        #4
        Originally posted by johny10151981
        donbock is right in his way.

        But you can convert it to switch statement.
        Simple way would be.
        get a new variable, Integer will be perfect for this.
        and then assign the value of integer under the same condition you have made and then you just need to use your integer number in your switch, so far I think, entire process is pointless
        Do you mean that I assign each value of the (node_kind) field to an integer value and then compare using it or what !!?

        Comment

        Working...