我写了一个函数,它缩短了所需长度的字符串(单词的句子).我不希望句子的切口碰巧出现在一个单词的中间.所以我跳过n个字符,直到我到达一个空格并在那里剪切句子字符串.我的问题不是一个真正的问题,编译我的函数时会发出一条警告:"警告:未使用计算值",请参阅代码中的注释行.该功能虽然按预期工作.所以,无论是我是盲人,还是我对我的项目都持不同意见,实际上我并不理解这个警告.有人可以指点我的功能缺陷吗?
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;
}
Run Code Online (Sandbox Code Playgroud)
我在开发机器上的编译器是"gcc版本4.2.4(Ubuntu 4.2.4-1ubuntu4)"
cas*_*nca 13
警告是由于*(取消引用) - 您没有在任何地方使用取消引用的值.做到这一点:
p--;
Run Code Online (Sandbox Code Playgroud)
并且警告应该消失.