I get a warning, not an error, and when I ignore the warning and build/run anyway, my code seems to be working just fine. I am still worried, though, because of the warning, or maybe I just want to understand it.
This function appends a single char to the beginning of a string.
void s_pfx(char pf, char *s){
i = -1;
while(s[++i]);
s[i + 1] = '\0';
while(i)
s[i] = s[--i];
s[i] = pf;
}
The warning is at the while loop, when I pre-decrement i. (I have i as a global variable.)
The warning is "warning: operation on 'i' may be undefined [-Wsequence-point]"
I can fix it by using
while(i){
s[i] = s[i - 1];
i--;
}
but why should I have to do it that way? It seems that would eat up time, nanoseconds maybe, but it will add up.
I don't understand why the compiler can't understand the code. It seems pretty well-defined to me. I looked at some of the other similar questions/answers but this seems a lot simpler.
Help! Thanks.