The value "pointer" is not used "in function c

I wrote a function that cuts a string (sentence of words) by the required length. I do not want the phrase abbreviation to be in the middle of a single word. So I skip n characters until I get to the place and cut the sentence line. My problem is not really a problem, compiling my function spits out a warning saying that "warning: the calculated value is not used", see the Commented line in the code. The function works as expected. Therefore, either I am blind, or I sit for a long time in my project, in fact I do not understand this warning. Can someone point me to a lack of 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)"

+4
source share
3 answers

The warning is caused by * (dereferencing) - you are not using the dereferenced value anywhere. Just do it:

 p--; 

and the warning should disappear.

+8
source

*p-- matches *(p--) .

The decrement is evaluated, then you play the result of this. In fact, you are not doing anything with this result, so a warning. You would get the same warning if you said *p .

+4
source

You just do it for a side effect

  *p--; 
  • Side effect: decrement p
  • Expression value: value pointed to by p

The compiler warns you that the value of the expression is not used :-)

+3
source

All Articles