CharlesLiuChina
CharlesLiuChina

Reputation: 271

Is it normal for strstr()?

Description:

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, "://");

Question:

Where do things go wrong?

Upvotes: 0

Views: 147

Answers (1)

AnT stands with Russia
AnT stands with Russia

Reputation: 320381

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*.

Upvotes: 9

Related Questions