我写了一个函数,它可以缩短请求长度的字符串(单词句)。我不希望这个句子的切割恰好在一个单词的中间。所以我跳回n个字符,直到我到达一个空间并在那里切断了这个句子。 我的问题是不是真的有问题,编译我的函数吐出一个警告,说:“警告:值计算未使用”,请参阅代码中的注释行。该功能按预期工作。 因此,无论我是盲人还是坐在我的项目上,实际上我都不明白这个警告。请问任何人都可以指出这个功能的缺陷吗?c函数中指针“未使用计算值”
char *
str_cut(char *s, size_t len) {
char *p = NULL;
int n = 3;
p = s + len;
if (p < (s + strlen (s))) {
/*
* do not cut string in middle of a word.
* if cut-point is no space, reducue string until space reached ...
*/
if (*p != ' ')
while (*p != ' ')
*p--; // TODO: triggers warning: warning: value computed is not used
/* add space for dots and extra space, terminate string */
p += n + 1;
*p = '\0';
/* append dots */
while (n-- && (--p > s))
*p = '.';
}
return s;
}
我在开发机器上编译器是 “gcc版本4.2.4(Ubuntu的4.2.4-1ubuntu4)”
为什么你会用一段时间? (只是好奇) – Blam 2010-12-03 16:40:20
另外,它应该检查该循环中的“p> s”(就像它在第二个中那样)。 – Lars 2010-12-03 16:45:37
@Blam:因为它是Alpha-Code,而且还不是很干净。是的,那个“如果”是不需要的。谢谢:-) @lars:真的,谢谢,我刚刚添加了! – 2010-12-03 17:00:30