0

我正在使用以下函数维护一些丑陋的遗留代码,我得到了

warning: value computed is not used

对于以下注释标记的行:

void ReadKeyValuePipe(char* buffer, char* key, char* value) {
    char* pos;
    char key_str[1024];
    char* val = value;
    sprintf(key_str,"%s:",key);
    if((pos = strstr(buffer,key))) {
        pos += strlen(key_str);
        while (*pos && *pos != '|') {
            *val = *pos;
            *val++; // this is actually used
            *pos++; // so is this
        }
        *val = 0;
    }
}

当我删除这些行时,代码会中断。这是有道理的,因为它们似乎是递增的标记。

我如何让编译器识别出这些计算是实际使用的?

4

2 回答 2

4

您正在取消引用val然后pos递增它们,但您从不使用取消引用的结果。您可以将这些行替换为:

*val = *pos;
++val;
++pos;

或者,可能在有人“修复”它之前代码最初是什么:

*val++ = *pos++;
于 2014-10-28T18:28:03.553 回答
1

-Wno-unused-value如果您确定您的代码是正确的,您可以使用编译器选项。这不是,因为你显然不知道做什么*val++;

*val++; 
*pos++; 

是相同的:

 val++;
 pos++;
于 2014-10-28T18:24:54.423 回答