I wan't to initialize a static char buffer with a string str in the code below but I am getting the following error :
error: cannot convert ‘std::string’ to >‘char’ in initialization
and if I use
static char buf[500] = str.c_str();
I get the following error:
error: invalid conversion from ‘const char*’ to ‘char*’
Below is my code :
std::string str = "<Version="+version+" Ret=\"false\"/>";
static char buf[500] = str;
int len=strlen(buf);
buf[len]='\0';
INFO("Static Buffer :: "<<buf);
First of all, you cannot directly initialise a char[] from an std::string. It's just not possible. Even if you could, you would write = str, not = { str }.
So, you need to create the array first then assign the std::string's contents to it manually. Sadly, arrays are not assignable, so you're going to have to use an "algorithm" to do it.
Here we go:
const std::string str = "Hello world";
static char buf[500] = {};
std::copy(
// from the start of the string
std::begin(str),
// to the end of the string, or to 499 chars in, whichever comes first
std::begin(str) + std::min(str.size(), sizeof(buf)),
// into buf
std::begin(buf)
);
Yuck.
If you can, and this is likely the case, avoid it.
If you really need a C-string with the std::string's contents, just access str.c_str() whenever you need to. There is, in general, no need to keep a raw char array lying about, especially when you already have the right tool for the job doing that job.
Besides, as you are not initialising buf with that data, if it's function-static, this code probably does not have the intended effect.
You can use std::string::copy().
std::string text = "Hello there!";
char* cStrText = new char[text.length()];
//Copy the string into the buffer
text.copy(cStrText, text.length(), 0);
Obviously, this performs a copy which may not be optimal. You might want to look into trying to move it.
https://www.cplusplus.com/reference/string/string/copy/
Related
I want to know more about programming and after a bit of googling I found how to convert a string to a const char.
String text1;
What I do not understand is why c_str() works,
const char *text2 = text1.c_str();
contrary to toCharArray()?
const char *text2 = text1.toCharArray();
or
const char text2 = text1.toCharArray();
The latter is more logical to me as I want to convert a string to a char, and then turn it into a const char. But that doesn't work because one is a string, the other is a char. The former, as I understand, converts the string to a C-type string and then turns it into a const char. Here, the string suddenly isn't an issue anymore oO
.
a) Why does it need a C-type string conversion and why does it work only then?
b) Why is the pointer needed?
c) Why does a simple toCharArray() not work?
.
Or do I do something terribly wrong?
Thanks heaps.
I am using PlatformIO with Arduino platform.
If you need to modify the returned c-style string in any way, or have it persist after you modify the original String, you should use toCharArray.
If you only need a null-terminated c-style string to pass as a read-only parameter to a function, use c_str.
Arduino reference for String.toCharArray()
Arduino reference for String.c_str()
The interface (and implementation) of toCharArray is shown below, from source
void toCharArray(char *buf, unsigned int bufsize, unsigned int index=0) const
{ getBytes((unsigned char *)buf, bufsize, index); }
So your first issue is that you're trying to use it incorrectly. toCharArray will COPY the underlying characters of your String into a buffer that you provide. This must be extra space that you have allocated, either in a buffer on the stack, or in some other writable area of memory. You would do it like this.
String str = "I am a string!";
char buf[5];
str.toCharArray(buf, 5);
// buf is now "I am\0"
// or you can start at a later index, here index 5
str.toCharArray(buf, 5, 5);
// buf is now "a st\0"
// we can also change characters in the buffer
buf[1] = 'X';
// buf is now "aXst\0"
// modifying the original String does not invalidate the buffer
str = "Je suis une chaine!";
// buf is still "aXst\0"
This allows you to copy a string partially, or at a later index, or anything you want. Most importantly, this array you copy into is mutable. We can change it, and since it's a copy, it doesn't affect the original String we copied it from. This flexibility comes with a cost. First, we have to have a large enough buffer, which may not be known at compile time, and takes up memory. Second, that copying takes time to do.
But what if we're calling a function that just wants to read a c-style string as input? It doesn't need to modify it at all?
That's where c_str() comes in. The String object has an underlying c-string type array (yes, null terminator and all). c_str() simply returns a const char* to this array. We make it const so that we don't accidentally change it. An object's underlying data should not be changed by random functions outside of its control.
This is the ENTIRE code for c_str():
const char* c_str() const { return buffer; }
You already know how to use it, but to illustrate a difference:
String str = "I am another string!";
const char* c = str.c_str();
// c[1] = 'X'; // error, cannot modify a const object
// modifying the original string may reallocate the underlying buffer
str = "Je suis une autre chaine!";
// dereferencing c now may point to invalid memory
Since c_str() simply returns the underlying data pointer, it's fast. But we don't want other functions to be allowed to modify this data, so it's const.
I have declare buffer const char* buf;
Later on I want to re assign size using memset
buffer_len = 1024;
memset(buf, '\0', buffer_len);
buf[strlen(buf)-1]='\0';
gives error:
client.cpp:73:30: error: invalid conversion from ‘const void*’ to ‘void*’ [-fpermissive]
In file included from client.cpp:2:0:
/usr/include/string.h:62:14: error: initializing argument 1 of ‘void* memset(void*, int, size_t)’ [-fpermissive]
client.cpp:75:21: error: assignment of read-only location ‘*(buf + (((sizetype)strlen(buf)) + -1u))’
I know it's due to const but is there any alternative or way to perform it event it is const?
The assignment buf[strlen(buf)-1]='\0'; is invalid because you defined buf as const: const char* buf; Read compiler's error message: error: assignment of read-only location.
One point: You set buf with nul \0 so length of buf is 0 (\0 at zero index) then if suppose you don't declare buf as const even then you would be assiging at negative index because strlen(buf) - 1 == 0 - 1 = -1 – Undefined behaviour
memset does not assign size. It fills a buffer with bytes. Filling a buffer declared as const char* makes no sense, since the reason you declare it const is for yourself not to write to it.
You could create a different array instead, since this const does not prevent you from changing the pointer itself.
Reassigning size should probably be called reallocating memory, you can use one of malloc, calloc or others to do it. Or since you've tagged this with c++, probably using the new operator would be the best idea.
Obvious the author is asking a solution for what he want to operate. And simply explanation of the comping error is satisfactory. I do not want to explain why the author's code is not compiling, cause the above answers explain very well about this.
I am try to give a solution if the author really want to do all the operations he meant in his code, though I do not recommend to do that in real world software project.
Use const_cast to remove the const property of the variable:
const char* buf;
...
buffer_len = 1024;
...
char *ptr = const_cast<char *>(buf);
memset(ptr, '\0', buffer_len);
ptr[strlen(buf)-1]='\0';
A way to achieve flexible strings in C is to use realloc:
//Get some malloc'ed strings.
char* myString = asprintf("my cool, malloc()-allocated string\n");
char* myOtherString = asprintf("Oh, and by the way: Hello World!\n");
//Make room in myString to append the other string.
size_t finalStringSize = strlen(myString) + strlen(myOtherString);
myString = realloc(myString, finalStringSize + 1); //+1 for termination
//Concatenate the strings
strcat(myString, myOtherString);
free(myOtherString), myOtherString = NULL;
But, of course, using C++ std::strings should be less of a hassle.
I have done a search in google and been told this is impossible as I can only get a static char * from a string, so I am looking for an alternative.
Here is the situation:
I have a .txt file that contains a list of other .txt files and some numbers, this is done so the program can be added to without recompilation. I use an ifstream to read the filenames into a string.
The function that they are required for is expecting a char * not a string and apparently this conversion is impossible.
I have access to this function but it calls another function with the char * so I think im stuck using a char *.
Does anyone know of a work around or another way of doing this?
In C++, I’d always do the following if a non-const char* is needed:
std::vector<char> buffer(str.length() + 1, '\0');
std::copy(str.begin(), str.end(), buffer.begin());
char* cstr = &buffer[0];
The first line creates a modifiable copy of our string that is guaranteed to reside in a contiguous memory block. The second line gets a pointer to the beginning of this buffer. Notice that the vector is one element bigger than the string to accomodate a null termination.
You can get a const char* to the string using c_str:
std::string str = "foo bar" ;
const char *ptr = str.c_str() ;
If you need just a char* you have to make a copy, e.g. doing:
char *cpy = new char[str.size()+1] ;
strcpy(cpy, str.c_str());
As previous posters have mentioned if the called function does in fact modify the string then you will need to copy it. However for future reference if you are simply dealing with an old c-style function that takes a char* but doesn't actually modfiy the argument, you can const-cast the result of the c_str() call.
void oldFn(char *c) { // doesn't modify c }
std::string tStr("asdf");
oldFn(const_cast< char* >(tStr.c_str());
There is c_str(); if you need a C compatible version of a std::string. See http://www.cppreference.com/wiki/string/basic_string/c_str
It's not static though but const. If your other function requires char* (without const) you can either cast away the constness (WARNING! Make sure the function doesn't modify the string) or create a local copy as codebolt suggested. Don't forget to delete the copy afterwards!
Can't you just pass the string as such to your function that takes a char*:
func(&string[0]);
I have an error in my program: "could not convert from string to char*". How do I perform this conversion?
If you can settle for a const char*, you just need to call the c_str() method on it:
const char *mycharp = mystring.c_str();
If you really need a modifiable char*, you will need to make a copy of the string's buffer. A vector is an ideal way of handling this for you:
std::vector<char> v(mystring.length() + 1);
std::strcpy(&v[0], mystring.c_str());
char* pc = &v[0];
Invoke str.c_str() to get a const char*:
const char *pch = str.c_str();
Note that the resulting const char* is only valid until str is changed or destroyed.
However, if you really need a non-const, you probably shouldn't use std::string, as it wasn't designed to allow changing its underlying data behind its back. That said, you can get a pointer to its data by invoking &str[0] or &*str.begin().
The ugliness of this should be considered a feature. In C++98, std::string isn't even required to store its data in a contiguous chunk of memory, so this might explode into your face. I think has changed, but I cannot even remember whether this was for C++03 or the upcoming next version of the standard, C++1x.
If you need to do this, consider using a std::vector<char> instead. You can access its data the same way: &v[0] or &*v.begin().
//assume you have an std::string, str.
char* cstr = new char[str.length() +1];
strcpy(cstr, str.c_str());
//eventually, remember to delete cstr
delete[] cstr;
Use the c_str() method on a string object to get a const char* pointer. Warning: The returned pointer is no longer valid if the string object is modified or destroyed.
Since you wanted to go from a string to a char* (ie, not a const char*) you can do this BUT BEWARE: there be dragons here:
string foo = "foo";
char* foo_c = &foo[0];
If you try to modify the contents of the string, you're well and truly on your own.
If const char* is good for you then use this: myString.c_str()
If you really need char* and know for sure that char* WILL NOT CHANGE then you can use this: const_cast<char*>(myString.c_str())
If char* may change then you need to copy the string into something else and use that instead. That something else may be std::vector, or new char[], it depends on your needs.
std::string::c_str() returns a c-string with the same contents as the string object.
std::string str("Hello");
const char* cstr = str.c_str();
How to assign a string to a char* (char pointer) in C++?
char *pw = some string
For constant initialization you can simply use
const char *pw = "mypassword";
if the string is stored in a variable, and you need to make a copy of the string then you can use strcpy() function
char *pw = new char(strlen(myvariable) + 1);
strcpy(pw, myvariable);
// use of pw
delete [] pw; // do not forget to free allocated memory
If you just want to assign a string literal to pw, you can do it like char *pw = "Hello world";.
If you have a C++ std::string object, the value of which you want to assign to pw, you can do it like char *pw = some_string.c_str(). However, the value that pw points to will only be valid for the life time of some_string.
If you mean a std::string, you can get a pointer to a C-style string from it, by calling c_str. But the pointer needs to be const.
const char *pw = astr.c_str();
If pw points to a buffer you've previously allocated, you might instead want to copy the contents of a string into that buffer:
astr.copy(pw, lengthOfBuffer);
If you're starting with a string literal, it's already a pointer:
const char *pw = "Hello, world".
Notice the const again - string literals should not be modified, as they are compiled into your program.
But you'll have a better time generally if you use std::string everywhere:
std::string astr("Hello, world");
By the way, you need to include the right header:
#include <string>
I think you may want to do this:
using namespace std;
string someString;
geline(cin,someString);
char *pw = strdup(someString.c_str());
But consider doing it another way. Check out http://tiswww.case.edu/php/chet/readline/rltop.html (GNU Readline library). I don't know details about it, just heard about it. Others may have more detailed or other tips for reading passwords from standard input.
If you only want to use it for a single call for something you do not need to copy the contents of someString, you may use someString.c_str() directly if it is required as const char *.
You have to use free on pw some time later,
String must be enclosed in double quotes like :
char *pStr = "stackoverflow";
It will store this string literal in the read only memory of the program.
And later on modification to it may cause UB.