繁体   English   中英

strcmp 为两个相同的字符串返回不正确的值

[英]strcmp returning incorrect values for two identical strings

我在 C 中有一个 parser() 函数 - 它接受一个输入字符串, - 标记它 - 将它存储在一个数组中,并将第一个值与 - 3 个不同数组中的所有元素进行比较 - 我使用 strcmp 函数进行比较,但是它似乎根本不起作用。

    int parser(char* stream) {
    char* aCouple[3] = {"neg","swp","sav"} ;
    const char* hCouple[2] = {"add","sub"} ;
    char* pCouple[1]= {"mov"};
    int i;
    int index = 0;
    char* p = strtok (stream, " ");
    char* array[3];
    while (p != NULL) {
        array[i++] = p;
        index = index + 1;
        p = strtok (NULL, " ");
    printf("parser detects: %s %s %s\n", array[0], array[1], array[2]);
    }
    for(i =0; i <=2; i++) {
        printf("comparing with index value %u i.e the element called %s \n", i, aCouple[i]);
        if (strcmp(array[0],aCouple[i]) == 0) {
            printf("aCouple detected\n");
            return 0;
        }
    }
    for (i =0; i <=1; i++) {
        printf("comparing with index value %u i.e the element called %s \n", i, hCouple[i]);
        if (strcmp(array[0],hCouple[i]) == 0) {
            printf("hCouple detected\n");
            return 0;         
        }
    }
    for (i =0; i<= 0; i++) {
        printf("comparing with index value %u i.e the element called %s \n", i, pCouple[i]);
        if (strcmp(array[0],pCouple[i]) == 0) {
            printf("pCouple detected\n");
            return 0;
        } else {
            printf("failed to find a single match\n");
        }
    }
}

该函数应该返回一条消息,说明检测到 aCouple、检测到 hCouple 或检测到 pCouple - 从给定的输入 - 但是这是我运行它时发生的情况:

swp
parser detects: swp
comparing with index value 0 i.e the element called neg 
comparing with index value 1 i.e the element called swp 
comparing with index value 2 i.e the element called sav 
comparing with index value 0 i.e the element called add 
comparing with index value 1 i.e the element called sub 
comparing with index value 0 i.e the element called mov 
failed to find a single match


预期行为如下:

instruction: 
swp
parser detects: swp
comparing with index value 0 i.e the element called neg 
comparing with index value 1 i.e the element called swp 
hCouple detected

重现行为所需的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct w2a {
    int w2at;
    int w2ab;
    int w2al;
    int w2ar;
};
struct node  {
    int coreNo;
    int accValue;
    int secValue;
    struct w2a w2a;
} core0, core1, core2, core3;

int parser(char* stream) {
    char* aCouple[3] = {"neg","swp","sav"} ;
    const char* hCouple[2] = {"add","sub"} ;
    char* pCouple[1]= {"mov"};
    int i;
    int index = 0;
    char* p = strtok (stream, " ");
    char* array[3];
    while (p != NULL) {
        array[i++] = p;
        index = index + 1;
        p = strtok (NULL, " ");
    printf("parser detects: %s %s %s\n", array[0], array[1], array[2]);
    }
    for(i =0; i <=2; i++) {
        printf("comparing with index value %u i.e the element called %s \n", i, aCouple[i]);
        if (strcmp(array[0],aCouple[i]) == 0) {
            printf("aCouple detected\n");
            return 0;
        }
    }
    for (i =0; i <=1; i++) {
        printf("comparing with index value %u i.e the element called %s \n", i, hCouple[i]);
        if (strcmp(array[0],hCouple[i]) == 0) {
            printf("hCouple detected\n");
            return 0;         
        }
    }
    for (i =0; i<= 0; i++) {
        printf("comparing with index value %u i.e the element called %s \n", i, pCouple[i]);
        if (strcmp(array[0],pCouple[i]) == 0) {
            printf("pCouple detected\n");
            return 0;
        } else {
            printf("failed to find a single match\n");
        }
    }
}

int main() { 
    core0.coreNo = 0;
    core0.secValue = 33;
    core0.accValue = 200;
    core0.w2a.w2at = 0;
    core0.w2a.w2al = 0;
    core0.w2a.w2ar = 1;
    core0.w2a.w2ab = 1;

    core1.coreNo = 1;
    core1.accValue = 0;
    core1.secValue = 0;
    core1.w2a.w2at = 0;
    core1.w2a.w2al = 1;
    core1.w2a.w2ar = 1;
    core1.w2a.w2ab = 1;
    printf("core0's accumulator value is currently: %u\n", core0.accValue);
    printf("core0's secondary value is currently: %u\n", core0.secValue);
    char instruction[512];
    printf("instruction: \n");
    fgets(instruction,512,stdin);
    parser(instruction);
  
} 

appended char* p = strtok (stream, " "); p = strtok (NULL, " "); to char* p = strtok (stream, " \\n"); p = strtok (NULL, " \\n"); 解决了这个问题

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM