我正在为汇编程序编写编译器,并且需要对从文件中获取的文本进行解析,而无需对原始String进行任何更改。我用来复制String的函数是strcpy到缓冲区,而用来剪切String的函数是strtok来削减缓冲区。
一切正常,但是一旦我尝试使用功能addressingConstantIndex剪切原始的String,我就会得到null

我试图将缓冲区的类型更改为Character的指针,但这实际上没有用。我想主要的问题是我将原始String复制到缓冲区的方式。

int main(){
    char desti[MAXCHAR];
    char *temp;
    char *token;
    temp = "mov LIST[5] , r4";
    strcpy(desti,temp);
    printf("\ndest is : %s\n", desti);

    token = strtok(desti," ");

    printf("\nthe Token in Main is : %s \n", token);

    token = strtok(NULL, ",");

    printf("\nthe Token in Main is : %s\n", token);

    printf("\nThe value is %d \n ",addressingConstantIndex(token));

    token = strtok(NULL, " ,");

    printf("\nthe Token in Main is : %s\n", token);
    return 0;
}



int addressingConstantIndex(char * str) {
    char buf[43];
    char *token;
    int ans;
    strcpy(buf, str);
    token = strtok(buf, "[");
    printf("The string is %s\n",str);
    if (token != NULL)
    {
        printf("the token is %s\n", token);
        token = strtok(NULL, "]");
        printf("the token is %s\n", token);
        if(isOnlyNumber(token))
        {
            token = strtok(NULL, " ");
            if (checkIfSpaces(token,0) == ERROR)
            {
                printf("ERROR: Extra characters after last bracket %s \n", str);
                ans = ERROR;
            } else
                ans = OK;
        } else {
            printf("ERROR: Unknown string - %s  - its not a macro & not a number.\n", token);
            ans = ERROR;
        }
    } else {
        printf("ERROR: %s , its not a LABEL", token);
        ans = ERROR;
    }
    return ans;
}


int isOnlyNumber(char *str) {
    int i, isNumber;
    i = 0;
    isNumber = 1;
    if (!isdigit(str[i]) && !(str[i] == '-' || str[i] == '+'))
    {
        isNumber = ERROR;
    }
    i++;
    while (i < strlen(str) && isNumber == 1)
    {
        if (!(isdigit(str[i]))) {
            if (isspace(str[i]))
                isNumber = checkIfSpaces(str, i);
            else
                isNumber = ERROR;
        }
        i++;
    }
    return isNumber;
}


int checkIfSpaces(char *str, int index) {
    int i;
    if (str == NULL)
    {
        return OK;
    } else {
        for (i = index; i < strlen(str); i++)
        {
            if (!isspace(str[i])) return ERROR;
        }
    }
    return OK;
}






预期结果:

dest is : mov LIST[5] , r4

the Token in Main is : mov

the Token in Main is : LIST[5]
The string is LIST[5]
the token is LIST
the token is 5

The value is 1

the Token in Main is : r4


实际结果:

dest is : mov LIST[5] , r4

the Token in Main is : mov

the Token in Main is : LIST[5]
The string is LIST[5]
the token is LIST
the token is 5

The value is 1

the Token in Main is : (null)



区别在于结果的最后一行。

最佳答案

问题在于strtok()维护着一个指向当前字符串位置的静态指针。因此,在addressingConstantIndex()中,您开始处理本地buf,以便当您返回main()时,不再解析desti,而是现在从buf解析addressingConstantIndex()

现有代码最简单的更改是使用strtok_r()(或Windows上的strtok_s()):

char* context = 0 ;
token = strtok_r( desti, " ", &context ) ;
printf("\nthe Token in Main is : %s \n", token);
token = strtok_r( NULL, ",", &context ) ;
...


然后类似地在addressingConstantIndex()中:

char* context = 0 ;
token = strtok_r(buf, "[", &context);
...

关于c - 如何在C中修复``strtok销毁原始字符串'',我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57445803/

10-15 16:48