Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I always wondered why one can't just convert null-terminated C string into std::string(without constructors, writing helper functions and other workarounds)? It appears to be a simple and overly commonplace problem, where you have, let's say argv[1] null-terminated string as an argument passed to your program(or any other naturally occurred C string) and you need to pass it's value to a function directly.
I.e
How do I wrap the cstr in-place without allocating new string object?
This is the answer as best I can tell.
const char * cstr = "Hello World.";
...
..myfunc(cstr);
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have a very basic question how can I assign a string value to a variable that already has integer value. For example, int a = 10 then I need to assign a ='hey'. Is it possible in c++?
I think you are new to C++? In C++ you assign the variable a fixed type (int in your case). It is not possibile to change that type inside the scope. You have to convert a to a string and save them in a new variable. How to convert a string (char* or std::string) you can look here:
Easiest way to convert int to string in C++
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
static const string FindSentinel() { return "<Dummy>"; }
static const string Void() { return "<Null>"; }
What does this do, and how can these functions be used?
Is "<Dummy>" and "<Null>" a keyword?
No. They are plain string literals.
The calling code may assign special meaning to those strings (if it wants to), but as far as the C++ language is concerned, they are just any old run-of-the-mill string literal - nothing to see here, move along.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I am having problems in understanding this statement. I don't know why this is not usual like others.
Assignment means giving a new value to an already existing object. Even though const char INITIAL='G'; has an = sign, it is not an assignment, because it is creating a new object, not modifying an existing one. char INITIAL; INITIAL='G'; would be an assignment, because INITIAL already exists when the new value is, well, assigned.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I would appreciate if someone could give me an example of how to convert char *variable to byte array and vice-versa in C++.
Thanks
If std::vector<char> is what you're after, then it's simply:
std::vector<char> byteArry(charPtr, charPtr + sizeOfCharPtr);
and the other way:
const char* charPtr = byteArry.data();
Have you tried anything you self, maybe google?
Anyways a char *variable is a char pointer, so you will need to know the size of the data and create a byte array in the same size.
After that you can do a memcpy
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
All my attempts so far have failed.Basically when I return a copy of the internal char array of the string, that copy has to be released, but I don't know how to release it.Wrapping it in a smart pointer doesn't work out, since it's destructor gets called immediately after I return it.Must I implement something like a garbage collector just for the immutable string?
const std::string will be fine.