0

Somehow my switch statement goes through none of my cases, but shouldn't it go in one? (I am using https://stackoverflow.com/a/4014981/960086 as a reference).

There is no output, and application is blocked after.

#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);
}
Community
  • 1
  • 1
Juan
  • 521
  • 1
  • 11
  • 28
  • 1
    What is the value of `x`? – Anish Ramaswamy Mar 08 '13 at 13:49
  • 2
    That shouldn't even compile with all those mis-matched brackets.. and without `break`s if it went into one, it should fall through into all of them – Mike Mar 08 '13 at 13:50
  • 1
    you have forgotten "break;" in all the cases. – Peter Miehle Mar 08 '13 at 13:51
  • possible duplicate of [best way to switch on a string in C](http://stackoverflow.com/questions/4014827/best-way-to-switch-on-a-string-in-c) – hyde Mar 08 '13 at 13:52
  • How would the lack of a `break` be his problem? It might certainly be AN issue. – Anish Ramaswamy Mar 08 '13 at 13:52
  • that's why i made a comment instead of an answer. Youst to make clear, the code posted is not the code the OP tried for himself – Peter Miehle Mar 08 '13 at 13:53
  • First apologies for the extra } on my code they are commented but copy/paste failed me. I added the breaks to each case, but still same result. My ideal scenario would be to use strings in the "case: STRING" but its not possible (for what i read) so I use that other questions to change from string to int. X is a correct string, i.e. string1, or string2. – Juan Mar 08 '13 at 13:58
  • 1
    Don't know if it helps with the overall problem, but your `printf("Case: nothing happen\n");` is unreachable code, which is why it's not being executed when the rest of the cases fail. You should have it under a `default` case. – jonhopkins Mar 08 '13 at 13:59
  • I added that printf because I figure, maybe the whole switch is broken. I have tried taking that one out with the same results. – Juan Mar 08 '13 at 14:01
  • 1
    Check the return value of `keyfromstring(x)` (take it out of the switch). It looks like it is not returning a matched value and you don't have a `default` case. – teppic Mar 08 '13 at 14:07
  • @teppic yes, i can now say thats the problem. Not sure how to fix it, as you see I even linked this question to the suggested "This question may already have an answer here:", which doesnt have an answer. I am trying to follow the most voted one. – Juan Mar 08 '13 at 17:35

2 Answers2

5
  • Your lookuptable[] has a space after "string1" which is inconsistent with the other entries. I have a feeling you didn't want this.
  • Your keyfromstring() is incrementing sym wrong (this causes a segfault). Replace with:

int keyfromstring(char *key)
{
    int i;
    for (i=0; i < NKEYS; i++) {
        t_symstruct *sym = lookuptable + i;
        if (strcmp(sym->key, key) == 0)
            return sym->val;
    }
    return BADKEY;
}

OR

int keyfromstring(char *key)
{
    int i;
    for (i=0; i < NKEYS; i++) {
        if (strcmp(lookuptable[i].key, key) == 0)
            return lookuptable[i].val;
    }
    return BADKEY;
}

  • Put your printf("Case: nothing happen\n"); inside a default.
Anish Ramaswamy
  • 2,326
  • 3
  • 32
  • 63
  • Good call. I was trying to figure out where the segfault was coming from, but didn't think the incrementation was it. – jonhopkins Mar 08 '13 at 14:19
  • Corrected those mistake, and created a new program with only these code (exactly). Still program crashes, but it is easier to see that it crashes after first loop of keyfromstring (where it should find the result). Copying the exact code to original question. – Juan Mar 08 '13 at 17:31
  • 1
    @Juan, Did you use a debugger to see where it is crashing? Because with these changes, your code works just fine for me. – Anish Ramaswamy Mar 09 '13 at 06:50
  • @AnishRam I am a little new with C, and I am using netbeans. Using the debugger it says I have a SIGSEGV (Segmentation fault) and then it pops a error: No registers. I also get a warning that keyfromstrings makes pointer from integer without a cast. – Juan Mar 09 '13 at 09:31
  • I edited code running at the moment and with the low level debugging of printing i think the error must be here 'if (strcmp(sym->key, key) == 0) { //creates the ERROR' – Juan Mar 09 '13 at 09:51
  • Foudn the error...Thanks for helping. It was a copy paste error. – Juan Mar 09 '13 at 12:01
-3

You can't do it that way. switch works for integer (constants), and your strings aren't any of those. Besides, look here, for this topic has already been discussed.

Community
  • 1
  • 1
bash.d
  • 13,029
  • 3
  • 29
  • 42