So I have already had a look at other posts with similar headings but none of the suggested answers are working for me.
I have a function that calculates the frequency of a char in a string:
int frequency(char *s, char c) {
int i;
for (i=0; s[i]; s[i]==c ? i++ : s++);
return i;
}
It works correctly but the compiler gives me the following error:
warning: pointer/integer type mismatch in conditional expression [enabled by default]
Could someone please explain why
Cheers
i++
is of type int
, while the type of s++
is char *
. In a conditional expression, you can't have two different types in the "then" and "else" branch, hence the warning.
Here the author of this code snippet was trying to be clever and short, but he simply got it wrong. I'd suggest rewriting this as
int frequency(const char *s, char c)
{
int i;
for (i = 0; s[i];)
if s[i] == c
i++;
else
s++;
return i;
}