-5

I believe it is because of the strcmp(). I tried it multiple ways so far and this is just the latest. My objective is to get the index of the array so I can go on to a switch statement to execute code. Any help would be appreciated, although I'm only expecting a "You can't do that."

Big picture is to incorporate this snip of code into a "utility" file that has multiple "functions" and would call it like util("Ping") to execute a Ping and so on...

int main(){
char *cmd = "Ping";
char *names[3]={"Ping","Stop","Go"};

int index = 3;
int i;
char *test;

for (i = 0; i < 44; i++)
{
    test = names[i];
    if (!strcmp(cmd,test))
    {
        index = i;
    }
}
printf("%s is index of %d\n",cmd,index);}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Kyle
  • 27
  • 1
  • 8
  • 6
    Because you are looping over 44 elements instead of 3? – Eugene Sh. Jun 23 '16 at 17:21
  • Ill edit that, there will be 44 items in the switch but I dumbed it down to post. – Kyle Jun 23 '16 at 17:23
  • Does it segfault for 3 as well? I don't think so. – Eugene Sh. Jun 23 '16 at 17:24
  • When you fix the loop limit it works fine: http://ideone.com/bsKjZU – Barmar Jun 23 '16 at 17:25
  • 3
    You should probably break out of the loop once you find a match. Why keep searching? – Barmar Jun 23 '16 at 17:25
  • I just tried your code and I didn't get a segfault. I had an output "Ping is index of 0" – Mirakurun Jun 23 '16 at 17:27
  • 2
    Don't edit the question to make the problem invisible. – Jonathan Leffler Jun 23 '16 at 17:32
  • @JonathanLeffler His comment suggests that the real code actually does have 44 words in the array, and he just simplified it for the SO question. – Barmar Jun 23 '16 at 17:33
  • @Barmar but the edit *is* making the problem invisible. Or, to be precise, non-existent. – Eugene Sh. Jun 23 '16 at 17:34
  • 1
    @EdHeal: I don't see this is an enhancement. It should be `i < sizeof(names) / sizeof(names[0])` though. An additional improvement would be to `const char * const names[] =...`. – too honest for this site Jun 23 '16 at 17:34
  • @EugeneSh. That's true, but if he's to be believed then the problem was something else, which we can't see. – Barmar Jun 23 '16 at 17:36
  • Anyway, the major close-reason is the same. Just the minor (typo or not reproducible) changed. – too honest for this site Jun 23 '16 at 17:36
  • @Barmar: the original code had undefined behaviour that could lead to a crash; the revised code appeared to be OK. Making an MCVE ([MCVE]) is a good idea (as you know), but it must reproduce the problem. We can only debug the code that is presented — we can't reliably debug other code when we can't see the other code. The edit invalidated valid answers to the original question. The question could have been updated to note explicitly that the first version had 44 instead of 3 but the problem still occurs when the value is set to 3; a silent change is [NBG](http://acronymfinder.com/NBG.html). – Jonathan Leffler Jun 23 '16 at 17:37

1 Answers1

1

Why do loop it for 44 times I don't get it. But when I changed 44 to 3 in the loop it works.

for (i = 0; i < 3; i++)

Depends on your compiler though but in case of 44 iteration windows gives me message application not responding.

Denis
  • 1,219
  • 1
  • 10
  • 15