CharlesLiuChina CharlesLiuChina - 1 year ago 101
C++ Question

Is it normal for strstr()?


Declaration of strstr:

char *strstr(const char *haystack, const char *needle);

Definition of my function:

hostinfo_t *extract_host_from_url(const char *url) {
/* ... */
char *scheme_pos = strstr(url, "://");
/* ... */

How I use it:

void rewrite_url(string &url) {
/* ... */
hostinfo_t * hostinfo = extract_host_from_url(url.c_str());
/* ... */

Error info:

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

     char *scheme_pos = strstr(url, "://");


Where do things go wrong?

Answer Source

C++ declaration of strstr, as given in <cstring> is overloaded

  const char* strstr( const char* str, const char* target );
        char* strstr(       char* str, const char* target );

With your set of arguments, you are calling the first function, which is why the return type is const char*.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download