strtok() 函数错误地标记了字符串

strtok() function is tokenizing the string incorrectly

我正在用 C 实现 pass 2 汇编程序并尝试使用 strtok() 获取表达式的项。该函数在 while 循环中正确地 运行 但它没有正确生成标记。

生成令牌的代码如下:

char *terms[50];
char *operand="THREE-3"
char delimit[] = "+-\*";
int k = 0;
terms[k] = strtok(operand,delimit);
while(terms[k] != NULL)
{
    printf("token [%d]=%s\n",k,terms[k]);
    k++;
    terms[k]=strtok(NULL,delimit);
}

这是输出:

token [0]=3
token [0]=3
token [0]=3
token [0]=3
token [0]=3
token [0]=3
token [0]=3
token [0]=3
token [0]=3
token [0]=3
token [0]=3
token [0]=3
token [0]=3
token [0]=3
token [0]=3
token [0]=THREE
token [1]=3
token [0]=THREE
token [0]=THREE
token [0]=THREE
token [0]=THREE
token [0]=THREE
token [0]=THREE
token [0]=THREE
token [0]=THREE
token [0]=THREE
token [0]=THREE
token [0]=THREE
token [0]=THREE
token [0]=THREE
token [0]=THREE
token [0]=THREE
token [0]=THREE
token [0]=THREE
token [0]=THREE
token [0]=THREE
token [0]=THREE
token [0]=THREE

问题是参数 operand 不允许修改,而 strtok() 修改了它的第一个参数,因此,您的代码导致 undefined behavior.

相关注释:

  • operand 是指向字符串文字 "THREE-3".

    的指针

    引用 C11,章节 §6.4.5

    [...] If the program attempts to modify such an array, the behavior is undefined.

  • 对于 strtok() 行为,来自章节 §7.24.5.8(强调我的

    The strtok function then searches from there for a character that is contained in the current separator string. If no such character is found, the current token extends to the end of the string pointed to by s1, and subsequent searches for a token will return a null pointer. If such a character is found, it is overwritten by a null character, which terminates the current token. The strtok function saves a pointer to the following character, from which the next search for a token will start.

    这意味着,strtok() 修改了它的第一个参数。

最简单的解决方案是使 operand 成为一个数组并使用所需的字符串文字对其进行初始化,然后将其作为第一个参数传递给 strtok()

改变

char *operand = "THREE-3";  // here operand points to a string literal

char operand[] = "THREE-3";  // here operand is an array of chars terminated by a NUL char

strtok 修改字符串,修改字符串文字是未定义的行为。