Conversion of std:string to char* [closed] - c++

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I'm trying to convert a result string that I get by doing some data manipulation, eg:
std::string result = std::string(/* num_max */, '0').append(myDataModule->getId().c_str());
I'm getting an id from getId() (which returns an AnsiString) on my TDataModule, converting that to a std::string using AnsiString::c_str(), and appending that with zeros on the left (spaces that will remain) to my result string.
I need to use the value of result in a function that receives a char *:
void receiver(char * data, int num){
... <- A lot of code here
if( data != NULL )
{
strcpy( rdData, data); <-- rdData is an char *
...
}
}
Basically, in this function it will copy data to rdData and do some verifications.
When I run the code, an error appears when I call this function:
candidate function not viable: no known conversion from 'std::string' (aka 'basic_string') to 'char *' for 1st argument

When I run the code, an error appears when I call this function:
candidate function not viable: no known conversion from 'std::string' (aka 'basic_string') to 'char *' for 1st argument
You did not show the actual code that is trying to pass the std::string to the receiver() function, but the error message is very clear. You simply cannot assign a std::string directly to a non-const char*, which is exactly what the compiler is complaining about.
However, you can get a const char* from a std::string using its c_str() method, and then you can const_cast that to a char* (as long as the function does not try to modify the char data), eg:
std::string result = ...;
receiver(const_cast<char*>(result.c_str()), static_cast<int>(result.size()));
Or, you can simply use &result[0] instead (which is guaranteed in C++11 and later to be contiguous and null-terminated), or you can use result.data() in C++17 and later, eg:
std::string result = ...;
receiver(&result[0]/*result.data()*/, static_cast<int>(result.size()));
Or, you could simply change result from std::string to AnsiString, as its c_str() method returns a non-const char*, eg:
AnsiString result = AnsiString::StringOfChar('0', 8) + myDataModule->getId();
receiver(result.c_str(), result.Length());
Either way, if receiver() only needs to read from data and not modify its content, then it should be changed to take data as a const char* instead (especially since that is what strcpy() expects anyway), eg:
void receiver(const char * data, int num)
Then you can use result.c_str() as-is, no trickery, whether result is an AnsiString or a std::string.

Related

C++ convert std::string to char

I have following scenario:
struct MyStruct
{
char name[25];
char address[12];
}
Somewhere::SomeMethod(std::shared_ptr<SomeArgumentClass> args)
{
// GetName() and GetAddress() both return std::strings
::MyStruct newValue = {args->GetName(), args->GetAddress()};
// add newValue to some structure that needs to have chars
}
Error:
error C2440: 'initializing' : cannot convert from 'std::string' to 'char'
error C2440: 'initializing' : cannot convert from 'std::string' to 'char'
I am not able to get my std::string converted to a char.
Thanks a lot!
Firstly, your terminology is incorrect. You don't want to convert to char, which is a single character. I realise that the error message claims you're trying to convert to char, but that's because your incorrect code has confused the compiler into thinking you're trying to initialise individual elements of the array name. Be wary of the specifics of an error message when you write the wrong code, because the compiler cannot read your mind — it can only go off of what you've written!
In reality you want to copy the characters inside your std::string, into the array of char, that is your member.
Like so:
Somewhere::SomeMethod(std::shared_ptr<SomeArgumentClass> args)
{
const auto& name = args->GetName();
const auto& address = args->GetAddress();
::MyStruct newValue;
std::copy(std::begin(name), std::end(name), &newValue.name[0]);
std::copy(std::begin(address), std::end(address), &newValue.address[0]);
// add newValue to some structure that needs to have chars
}
But you need to add bounds checking too. To do that, roughly I might consider replacing each std::copy call with something like:
std::copy_n(
&name[0],
std::min(name.size(), sizeof(newValue.name)),
&newValue.name[0]
);
You'll also need some null termination depending on how many characters were copied.
Generally though the salient point is that you need to actually copy those characters because there is no one-step way to do it. It stems in part from how arrays can't be assigned-to, but also from the fact that their dimensions are part of the type which makes things more complicated.
If this seems like a pain in the arse, that's because it is — ideally you would stick with std::string across the board because it's far superior, and was specifically introduced to make it easier to handle arrays of char. You can still pass a const char* (or char*!) pointing to the string's data into a C API. Of course if ::MyStruct itself is a third-party C type, you have no choice but to do it the hard way.

printing a set containing strings on graphics.h in c++

I am trying to print the elements of a set containing strings on graphics.h console using outtext() function,but i get this error:
cannot convert 'std::string {aka std::basic_string}' to 'char*' for argument '1' to 'void outtext(char*)'|
this the piece of code that gives error:
for(i=0;i<20;i++){
for(j=0;j<20;j++){
outtext(str[i][j]);
}
}
the template for the outtext function in the graphics.h header is like this:
void outtext(char *textstring);
i have used c_str() like this:
for(i=0;i<20;i++){
for(j=0;j<20;j++){
outtext(str[i][j].c_str());
}
}
but this time it gives this error:
error: invalid conversion from 'const char*' to 'char*' [-fpermissive]|
You can try this one as well:
char *cstr = new char[21]; // just in case string length is maxed at 20, leave 1 character for '\0'
for (int i = 0; i<20; i++) {
for (int j = 0; j<20; j++) {
strcpy_s(cstr, str[i][j].length() + 1, str[i][j].c_str());
outtext(cstr);
}
}
delete[] cstr;
Just added a char* string to temporarily hold the converted std::string value. The tricky part is that char* strings normally have the terminating character \0 which std::string don't have, so you have to add 1 more character to the size of each "row" of str.
I take it this question is about the 30 years old BGI graphics library and Borland C++. The root of the problem is that this library was poorly written, as it didn't implement const correctness.
The Turbo C++ compiler did not follow anything remotely close to any C++ standard, so you are mostly out of luck. If you had a proper C++ compiler you could use const_cast, but I very much doubt this is available to you.
The only solution left is the dirty, bad way:
outtext((char*)str[i][j].c_str()); // bad practice
You should never cast away const like this in neither C nor C++.
If you can change the prototype of the output function then it is better to change void outtext(char *textstring); to void outtext(const char *textstring); because there is no need for the output function to modifiy the string. Otherwise you could use const_cast before passing to the function like outtext(const_cast<char*>(str[i][j].c_str())) or copy the string to another char* and passed the copied value.

C++: vector<string> to char** [duplicate]

This question already has answers here:
convert vector<string> into char** C++
(4 answers)
Closed 7 years ago.
I am converting my app to be able to be used as a library. For this purpose, I want to offer the possibility to pass a vector of strings to the library's default run routine.
The problem I am running into, is actually creating the char**. This is my current implementation, commented out in the source since it isn't working:
IceTea* IceTea::setupCli(vector<string> strings) {
int argc=strings.size(), i=0;
char* argv[argc];
vector<string>::iterator it;
for(it=strings.begin(); it != strings.end(); ++it) {
argv[i++] = (char*)it->c_str();
}
// Pass the char** to the original method.
return this->setupCli(argc, argv);
}
The error I get:
src/IceTea.cpp:132:18: error: no matching member function for call to 'setupCli'
return this->setupCli(argc, argv);
~~~~~~^~~~~~~~
src/IceTea.h:44:13: note: candidate function not viable: no known conversion from 'char *[argc]' to 'const char **' for 2nd argument
IceTea* setupCli(int, const char**);
^
src/IceTea.cpp:124:17: note: candidate function not viable: requires single argument 'strings', but 2 arguments were provided
IceTea* IceTea::setupCli(vector<string> strings) {
I am afraid you cannot convert a vector<string> to char** in constant time. A vector<string> can be converted to string* pretty easily, and string can be converted to char* pretty easily, but vector cannot be converted to char**. Actually, this is a problem very similar to why an int[10][20] cannot be trivially converted to an int**.
Now, if you absolutely have to do this, then
vector<char*> pointerVec(strings.size());
for(unsigned i = 0; i < strings.size(); ++i)
{
pointerVec[i] = strings[i].data();
} //you can use transform instead of this loop
char** result = pointerVec.data();
The main problem with your code is that you're using c_str which returns a const char *. Note that I'm using the data() member function instead, which returns the non-const pointer. Another difference between your attempt and my suggestion is that you're using an array of a length which is not a constant expression. That is illegal in standard C++. I used a vector instead and later converted it to a pointer with the same data() function.

C++: How to convert 'const char*' to char

I know there are a lot of questions like this out there on StackOverflow, but I haven't been able to find any that help resolve my case. Whenever I try to do something like this:
// str = some string or char array
// some magic to get around fpermissive errors
stringstream convert;
convert << str;
// capture the stream's temporary string
const string store = convert.str();
// get a manageable array
const char* temp = store.c_str();
and then try to do something like atoi(temp[0]), I keep getting the classic conversion error that char couldn't be converted to const char. In the documentation for atoi and many other functions, const char is a required parameter. How can a char be sent in if there's only a const one? Does retrieving a char at a specific position auto-cast to char?
I'm not sure if this is what is causing the error, but atoi takes as its parameter not a char, but the pointer to one. So instead of
atoi(temp[0])
try this
atoi(&temp[0])
as that is a pointer.

Deprecated conversion from string constant to char * error [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
C++ deprecated conversion from string constant to ‘char*’
I am having following code, though i didn't copy full code because it is huge.
Following code is in template class, and i am getting warning as below. Because of warning in template i am not able to instantiate it and getting "instantiated from here" error.
warning: deprecated conversion from string constant to 'char*''
void ErrorMessageInRaphsodyCode(char* pcCompleteMessage, char* pcMessage, char* pcFileName, unsigned int RowNo)
{
//...
}
char cCompleteMessage[200];
memset(cCompleteMessage, 0x00, sizeof(cCompleteMessage));
char*cMessage = "add reorgenize failed";
ErrorMessageInRaphsodyCode(cCompleteMessage, cMessage, "omcollec.h", __LINE__);
My question is what is best way to get rid of above warning ?
If a function takes a char const *, it guarantees that it only reads whatever data the pointer points to. However, if it takes a non-const pointer, like char *, it might write to it.
As it is not legal to write to a string literal, the compiler will issue a warning.
The best solution is to change the function to accept char const * rather than char *.
char cMessage[] = "add reorganize failed";
This should get rid of the warning.
Best way to get rid of it is to fix the function that is taking the parameter.
If your code is correct and the function does indeed take string constants, it should say so in its prototype:
void ErrorMessageInRaphsodyCode(char* pcCompleteMessage, char* pcMessage, const char* pcFileName, unsigned int RowNo)
If you can't do that (you don't have the code), you can create an inline wrapper:
inline void ErrorMessageInRaphsodyCodeX(char* p1, char* p2, const char* p3, unsigned int p4)
{ ErrorMessageInRaphsodyCode(p1,p2,(char*)p3,p4); }
and use the wrapper instead.
If your code is incorrect and the function does actually require writeable memory (which I highly doubt), you will need to make the string writeable by either creating a local array as Jan suggested, or mallocating enough memory.
(1) Make the variable a const char*
(..., const char* pcFileName, ...)
(2) If above is not possible and you want to retain the state of char* and const char* then make the function a template:
template<typename CHAR_TYPE> // <--- accepts 'char*' or 'const char*'
void ErrorMessageInRaphsodyCode(char* pcCompleteMessage, CHAR_TYPE* pcMessage, char* pcFileName, unsigned int RowNo)
{
//...
}
function c_str() of std::string class.