开发者

pointer "value computed is not used" in c function

开发者 https://www.devze.com 2023-01-28 13:59 出处:网络
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 un

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 ...
       开发者_StackOverflow中文版  */
        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--;
  • Side-effect: decrement p
  • Value of expression: value pointed to by p

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

0

精彩评论

暂无评论...
验证码 换一张
取 消