CharlesLiuChina CharlesLiuChina - 8 months ago 70
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?


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