Search code examples
c++stringcharstrchr

Why did I get error by using strchr() in C++?


#include <iostream>
#include <string>
#include <cstring>
using namespace std;

int main(){
    string a="asdasd";
    if(!strchr(a,'a')) cout<<"yes";
    return 0;
} 

I just began to learn C++ programming and I don't know why I got error in this line

if(!strchr(a,'a')) cout<<"yes";

But if I tried to code it like this, it would run very well.

if(!strchr("asdasd",'a')) cout<<"yes";

I know it is a stupid question but I really don't know why.. sorry..


Solution

  • The library function strchr is for use with C-style strings, not the C++ string type.