-2

So I have a string which contains "RGGB" and I need it to be in a char array to perform some operations. Then I need to replace certain characters for a blank space, for example the first 'G', so that my char array remains "R GB".

How can I do this? So far I tried this solution:

int main()
{
    string problem="RGGB";
    const char *p=problem.c_str();
    p[1]=' ';
    return p;
}

I get the error:

assignment of read only location *(p + ((sizetype)i))

Emil Laine
  • 41,598
  • 9
  • 101
  • 157
Puértolas Luis
  • 235
  • 1
  • 2
  • 13

4 Answers4

2

To access the "interal string" (I mean a const char*) of a std::string, there are two member functions provided: std::string::c_str and std::string::data. Until C++11, the difference was that std::string::data wasn't bound to return a pointer to a null-terminated const char* while std::string::c_str was. Now, they are equivalent. And both return a const char*, even before C++11.

There are several approaches to your problem:

  1. Use std::strdup or std::str(n)cpy to duplicate the string and write to the duplicate.

  2. Use a const_cast. Pretty drastic, but, if it doesn't hurt any rules (FYI, it does), works.

  3. Don't use std::string at all. Do what you want with a char* and then optionally convert it to a std::string later.

  4. Just use the functionality of std::string.

Community
  • 1
  • 1
cadaniluk
  • 15,027
  • 2
  • 39
  • 67
0

p[1]=' ';

is not valid as const char * is read-only pointer.

in other words, it is a const pointer.

remember:

When const appears to the left of the *, what's pointed to is constant, and if const appears to the right of the *, the pointer itself is constant. If const appears on both sizes, both are constants.

this code might work:

char problem[]="RGGB";
char* p = problem;
p[1]=' ';
cout<<problem;
vishal
  • 2,258
  • 1
  • 18
  • 27
0

string, a C++ class, does not provide directly alterable access to its innerds via a char *. While you could cast away the const, this is dangerous because compilers may use the const as an optimization path.

If you absolutely need to do this just use a char array, not a string or alter the contents of the string using string methods.

doug
  • 3,840
  • 1
  • 14
  • 18
  • "string […] does not provide directly alterable access to its innerds via a char *", what about `&str.at(0)`? – Emil Laine Oct 21 '15 at 17:10
  • str.at(0) provides a const reference as well. If the contents of a string were user alterable it would break compiler optimizations and memory layout sharing. You can always cast away const as C++ allows dangerous things but it makes you do it on purpose. It might work now on some or even all compilers but it could well fail to work on future compilers even from the same vendor. – doug Oct 22 '15 at 19:13
  • Uhm, `at` also has a [non-const overload](http://en.cppreference.com/w/cpp/string/basic_string/at). Taking the address from that yields a `char*` that you can use to modify the string _perfectly legally_. (as long as you don't add a `'\0'` in the middle or modify the terminating `'\0'`) – Emil Laine Oct 22 '15 at 19:23
  • I stand corrected. I noticed that str[0] also returns a non const ref. in addition to a const ref so it should be usable to directly write to a string element. Always assumed otherwise given that c_str() doesn't do that. I suppose it's so implementers can peel off a copy and add a null for C library functions without having to store a \0 at the end of each string. – doug Oct 22 '15 at 22:08
  • Don't string objects allow \0s in the middle? I thought the only reason to avoid that was for use with old C functions via c_str()? – doug Oct 22 '15 at 22:10
0

The answer is in your question:

I need it to be in a char array

Then put it in a char array:

char problem[] = "RGGB";
problem[1] = ' ';

Problem solved.

If, on the other hand, you want to solve the problem using actual C++:

std::string problem = "RGGB";
problem.at(1) = ' ';
Emil Laine
  • 41,598
  • 9
  • 101
  • 157