-2

I am trying to find the occurrence of characters of one string(s1) in other string(s2).

This is part of my code.

for(;i<strlen(s1);i++)
{
   int x=strchr(s2,s1[i]);
   if(x>0)
   count++;
}

But on compiling I get an error pointing to strchr() and says

error: invalid conversion from ‘char*’ to ‘int’ [-fpermissive]

Anyone explain what is problem in using strchr() function.

nomorequestions
  • 589
  • 2
  • 6
  • 20
  • 1
    The problem is that you didn't read its documentation. I'm wondering why would you **ever** try using a function when you **know** you have no idea what it is doing... –  Feb 08 '14 at 06:47

1 Answers1

2

Assignment is wrong strchr doesn't returns int but returns address of first char in string found:

int x=strchr(s2,s1[i]);

should be:

char* x = strchr(s2, s1[i]); 
//         ^ returns char* 

Read manual

char *strchr(const char *s, int c);

RETURN VALUE
The strchr() and strrchr() functions return a pointer to the matched character or NULL if the character is not found. The terminating null byte is considered part of the string, so that if c is specified as '\0', these functions return a pointer to the terminator.

And so:

if(x>0)

should be:

if(x != NULL)

or just if(x)

Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208