Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pointer "value computed is not used" in c function

Tags:

c

pointers

gcc

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)"

like image 775
Andreas W. Wylach Avatar asked Dec 03 '10 16:12

Andreas W. Wylach


3 Answers

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.

like image 196
casablanca Avatar answered Nov 06 '22 22:11

casablanca


*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.

like image 41
James McNellis Avatar answered Nov 06 '22 23:11

James McNellis


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 :-)

like image 44
pmg Avatar answered Nov 06 '22 22:11

pmg