c - strtok() 非常奇怪的行为

标签 c

编辑:新问题

    char * symbolInList = listOfSymbolTokens[0];
    char * addressOfSymbol = listOfSymbolTokens[1];

    printf("symbolLookingFor: %s\n", symbolLookingFor);
    printf("symbolInList: %s\n", symbolInList);
    printf("addressOfSymbol: %s\n", addressOfSymbol);
    if (strcmp(symbolInList, symbolLookingFor) == 0) {
        strcpy(tokenAddress, addressOfSymbol);
        return;
    }
    printf("did not enter for loop\n");

出于某种原因,strcmp(symbolinList, symbolLookingFor) 即使它们相同也不会返回 0。

symbolLookingFor: abc
symbolInList: abc
addressOfSymbol: 3011

did not enter for loop

不进入if语句

最佳答案

这是错误的

const char sdilem[1] = " ";

要存储 "" 字符串,缓冲区中至少需要 2 个字符。 (在 C 中这不是错误。)

首先,为了避免此类错误,您不需要显式指定大小

const char sdilem[] = " ";

其次,在这种情况下你可以使用

const char *sdilem = " ";

关于c - strtok() 非常奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40010351/

相关文章:

c - 释放结构内部的指针(指向 void*)

java - 如何在 CSV 文件或 Excel 文件中获取该目录内的文件路径、名称、创建日期、上次修改日期和文件扩展名?

c - 为什么 strlen() 不代表我的字符串的实际长度?

c - Timer1 作为实时时钟的精度,16F 上有 PIC 中断*

c++ - 零大小结构

c - 如何修复 "used struct type value where scalar is required"?

c - 个人使用程序的 MISRA-C 编码指南?

c - 返回逻辑运算符

c - 让多个线程同时读取文件的最佳方法是什么?

c - 用于编译和运行 .c 文件的 Visual Studio Code 任务