How to convert `const char *` to just `char *`?

I use the pdCurses library and try to use only strings in my console game in C ++, but the curses mvinstr() function or any insert function requires no char * constant as a parameter.

  • My solution at first for this problem was to simply put in string.c_str() , but that returns a const char * , which apparently doesn't work with this function.
  • Then I put (char *)string.c_str() , but this only raises an unhandled exception.
  • Finally, I just tried char *test = string.c_str() but is not compatible with const .

What should I do to solve this problem?

K I just tried const_cast () and I still get the exception and the break .... I don't know why PDcurses accepts only non-const char pointers .... = (

in the order in which the buffer was created, char * did not work when I used this code (time_s is a sting):

 size_t length; char buffer[12]; length=time_s.copy(buffer,5,0); buffer[length]='\0'; mvinstr(time_loc_y, time_loc_x, buffer); 

I even put an end to mvinstr () and checked the contents of the buffer, which was "00/0", EXACTLY WHAT I WANT.

but I get a "xutility" access violation point ....

+4
source share
7 answers

mvinstr(x,y,str) and others "take characters (or wide characters) from the current or specified position in the window and return them as a string to str (or wstr )."

The function will actually change the string, so you cannot safely drop const , especially since c_str indicates that you should not modify the returned string.

You need something like:

 const MAX = 100; char buf[MAX]; mvinnstr(x, y, buf, MAX); ...error checking... string s = buf; 

Note that I avoided mvinstr in favor of mvinnstr to avoid the possibility of buffer overflows.

+7
source

What about

 char* buffer = &str[0]; int fetched_len = mvinnstr(time_loc_y, time_loc_x, buffer, str.size()); str.resize(fetched_len); 

In general, you should make a rewritable buffer instead of removing const from the pointer that has it. eg.

 vector<char> charvec(MAX_LENGTH); str = string(&charvec[0], mvinnstr(time_loc_y, time_loc_x, &charvec[0], charvec.size()); 
+3
source

Caution - if code that uses was- const data tries to change it, anything can happen.

In short:

 const std::string str = "..."; char *caution = const_cast<char *>(str.c_str()); 

However, given that you get unhandled exceptions, you probably need to make a modifiable copy of the constant string before calling mvinstr() . May be:

 const std::string str = "..."; char *caution = new char[str.length()+1]; str.copy(caution, str.length()+1); ...call to mvinstr()... delete[] caution; 
+1
source

Since mvinstr actually stores the data in the array pointed to by char* , you cannot use string . You need to allocate the char array, pass it to mvinstr , and then transfer the characters to string if you want.

If you used a function that could be declared using const char * (i.e. it does not actually modify the array), you can use const_cast<> to remove const .

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

But that is not what you are doing here. const_cast may work if you tried it, but it would be an accident, and not because it should work, and a new version of the compiler or library may break it at any time.

+1
source

Try something like the following, then use buffer wherever you need char* . As Ben said, you need to be very careful that the buffer is larger than a line with a null terminator.

 const int BUFFER_SIZE = 255; string str ("Your string"); char buffer[BUFFER_SIZE]; if (str.length() < BUFFER_SIZE) { size_t copy_length; copy_length=str.copy(buffer,str.length(),0); buffer[copy_length]='\0'; } 
+1
source

Removing const can be done using const_cast and sometimes you need to use legacy interfaces written in C and not use const. In such cases, you can:

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

However, on the cplusplus c_str () help page:

The returned array indicates the internal location with the necessary space for this sequence of characters plus its terminating null character, but the values ​​in this array should not be changed in the program and are provided only if they remain unchanged until the next invocation of a mutable function is a member of a string object.

This means that you are responsible for ensuring that ptr not used to change the string, but must handle ptr lifetime accordingly. Ie, you cannot continue to use ptr after str goes out of scope or after calling any non-constant methods on str . If you do this, you will go to Undefined Behavior and most likely crash.

If you are looking for a safe way to interact with an obsolete interface that accepts char* , you can create your own writable copy:

 char* ptr = new char[ str.size() + 1 ]; strcpy(ptr, str.c_str()); ptr[str.size()] = '\0'; // ... use ptr as much as you want... delete [] ptr; 
0
source

if (string.empty()) foo(const_cast<char*>(string.c_str());

the best, but still evil way to remove const is const_cast (const_string); (it is better to find it through grep / search)

The witch exception you encountered? Are you just reading char * or are you changing the values? if you change, you must change your code.

const char* test = string.c_str(); Does not create a deep copy of the string, only a pointer to the internal representation of the data string.data ().

=> (suggestion) find a book in C ++ where you can get a deeper look at C ++. Or something like C ++.

-1
source

All Articles