I wrote a function that that shortens a string (sentence of words) at requested length. I do not want that a the cut of the sentence happens to be in middle of a single word. So i skip back n chars until I reach a space and cut the sentence string there. My problem is not really a problem, compiling my function spits out a warning that says "warning: value computed is not used", see the commented line in the code. The function works as expected though. So either I am blind, or I am sitting to long on my project, actually I do not understand that warning. Could anybody please point me the flaw in the function?
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;
}
My compiler on the development machine is "gcc version 4.2.4 (Ubuntu 4.2.4-1ubuntu4)"
The warning is due to the *
(dereference) -- you aren't using the dereferenced value anywhere. Just make it:
p--;
and the warning should go away.
*p--
is the same as *(p--)
.
The decrement is evaluated, then you dereference the result of that. You don't actually do anything with that result, hence the warning. You would get the same warning if you just said *p
.
You just do this for the side-effect
*p--;
The compiler is warning you the value of the expression is not used :-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With