Store c_str() as char *

人走茶凉 提交于 2021-02-07 19:54:18

问题


I'm trying to use the function with the following declaration: extern int stem(struct stemmer * z, char * b, int k)1

I'm trying to pass a C++ string to it, so I thought I'd use the c_str() function. It returns const char *. When I try to pass it to the stem() function, I get this error: error: invalid conversion from 'const char*' to 'char*' [-fpermissive].

How can I store the result of c_str() such that I can use it with the stem function?

Here is the code I'm running:

    struct stemmer * z = create_stemmer();
    char * b = s.c_str();
    int res = stem(z, b, s.length()); //this doesn't work
    free_stemmer(z);
    return s.substr(0,res);

回答1:


The problem you are having is that c_str() returns a buffer that can not be modified (const), while stem() may modify the buffer you pass in (not const). You should make a copy of the result of c_str() to get a modifiable buffer.

The page http://www.cplusplus.com/reference/string/string/c_str/ has more information on the C++ 98 and 11 versions. They suggest replacing char * b = s.c_str(); with the following:

char * b = new char [s.length()+1];
std::strcpy (b, s.c_str());



回答2:


You shouldn't try to remove constness of a string returned by c_str():

char * b = s.c_str();

but you can pass an address of std::string's internal buffer directly:

int res = stem(z, static_cast<char*>(&s[0]), s.length());



回答3:


If stem() is going to modify the string, then make a copy of it:

char * scpy= strdup( s.c_str()) ;
int res = stem(z, scpy, strlen( scpy)); 
free( scpy) ;



回答4:


Use const_cast:

int res = stem(z, const_cast<char*>(s.c_str()), s.length()+1);
free_stemmer(z);
return s.substr(0,res);

Note the length+1 expression which might (or might not) be needed. C-style strings (char*) have an additional null terminator (zero byte, equivalent "\0") at the end. Your stem function may (or may not) expect a null terminator at the end of the string - try both variants.

Note also that "stem" function should not try to modify the string, otherwise bad things may happen (warning based on @David Heffernan's comment)




回答5:


.c_str()

Just returns a pointer to the data, I would update the stem function to accept a 'const char*' unless you are wanting to modify the data in the string, in that case you should pass it as a new string object.

If you can't edit the stem function you can cast it:

int res = stem(z, const_cast<char*>(s.c_str()), s.length());



回答6:


It's not good to do this, but nothing stops you:

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

void foo(char *ch)
{
  ch[0] = 'B';
}

int main()
{
  string str = "helo world";

  char *ch = const_cast<char *>(str.c_str());

  foo(ch);

  // Belo world
  cout << str << endl;

  return 0;
}


来源:https://stackoverflow.com/questions/20305611/store-c-str-as-char

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!