0

I searched for a string replacement function and found this question

What is the function to replace string in C?

If I use the code from the answer, it works but it looks wrong and gives a warning:

/home/dac/osh/util.c: In function ‘str_replace’:
/home/dac/osh/util.c:867:5: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
     for (count = 0; tmp = strstr(ins, rep); ++count) {

It looks like it's maybe a bug with = and == . Is it a bug or did I misunderstand? Should it be == instead?

Community
  • 1
  • 1
Niklas Rosencrantz
  • 25,640
  • 75
  • 229
  • 424

3 Answers3

3

No, it's not. In this case, the value of tmp is actually intended to be used as the condition.

The return value of strstr:

char * strstr(char * str1, const char * str2 );

Returns a pointer to the first occurrence of str2 in str1, or a null pointer if str2 is not part of str1.


To remove the warning, try this:

for (count = 0; (tmp = strstr(ins, rep)) != NULL; ++count) {
Community
  • 1
  • 1
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
2

No, it's not a bug. As per the body of the loop:

for (count = 0; tmp = strstr(ins, rep); ++count) {
    ins = tmp + len_rep;
}

it actually uses tmp for something. The continuation condition in that for statement will assign the result of strstr() to tmp then execute the body as long as it's non-zero (i.e., as long as it found the string). That's because strstr() returns NULL only if the string cannot be found.

I suspect this is just gcc being paranoid in that it realises the continuation condition (the middle bit) on a for statement usually tends to be a comparison and you may have accidentally used = rather then ==.

That's why the diagnostic states warning: suggest ... rather than error: what the heck? :-) If you want to get rid of the warning (which isn't a bad position to take), simply do what it suggests and surround the entire continuation condition with parentheses.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
0

[ strstr reference ] states the return value for char * strstr (char *str1,const char *str2 )
is :

A pointer to the first occurrence in str1 of the entire sequence of characters specified in str2, or a null pointer if the sequence is not present in str1.

Now for a little more C terminology, when you do :

tmp = strstr(ins, rep)

the main intent of C is to evaluate the expression as a whole and C evaluates it to the return value of strstr(ins, rep) here. The side effect is assigning this return value to tmp. The error :

suggest parentheses around assignment used as truth value

is a way gcc helps you to avoid a careless mistake, say typing a=b instead of a==b, I believe. Note that in the first case the value of b is used as truth value, but in the second case the result of is a equal b is used a truth value. By putting a () around tmp = strstr(ins, rep) you give the compiler the green signal to evaluate the value of the expression as truth value.

Side Note :

Putting () around tmp = strstr(ins, rep) makes it a full expression, and full expression is considered as a sequence point. A sequence point is a point in program execution at which all side effects are evaluated before going on to the next step.

sjsam
  • 21,411
  • 5
  • 55
  • 102