我搜索了一个字符串替换函数,发现了这个问题
如果我使用答案中的代码,它可以工作,但看起来不对,并给出警告:
/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) {
似乎是=
and的错误==
。是错误还是我误会了?应该==
代替吗?
不,这不对。在这种情况下,tmp
实际上应将的值用作条件。
的返回值strstr
:
char * strstr(char * str1, const char * str2 );
返回指向
str2
in的第一个匹配项的str1
指针;如果str2
不属于in ,则返回null指针str1
。
要删除警告,请尝试以下操作:
for (count = 0; (tmp = strstr(ins, rep)) != NULL; ++count) {
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句