Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
i have std::string pointer and i like to copy its value to ordinary std::string
i can't find quick and fast way to do it .
i have this :
int main ()
{
std::string * pStr = new std::string("hello")
std::string strNew = pStr->??? // how to convert ?
return 0;
}
Dereference:
std::string strNew = *pStr;
Two ways:
std::string strNew = pStr->c_str(); // Be careful of `\0` with in the string
or
std::string strNew = *pStr;
The second is better, because C-style string can not represent std::string correctly. It ends a string at first \0 and ignores trailing.
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 10 months ago.
Improve this question
In c++11, I have a function that takes a const void* data and a size_t dataSize, so I have to pass to the function the pointer to a string.
function_test( const void* Data, size_t DataSize)
{
std::string received_data((const char*)Data, DataSize);
if (received_data=="1")
{
//do stuff... I need enter here
}
}
I use:
std::string test1 = "1";
function_test(&test1, sizeof(test1));
I expect that received_data is equal to "1" but it's not, why? How can I fix this?
Why test3 is not "1"?
Because std::string is not an array of char. A pointer to std::string is not a pointer to the buffer that std::string class manages where the content of the string is stored. When you reinterpret a pointer to a std::string object as a pointer to char, you get a meaningless representation of the internals of the std::string object.
I need that test3 is equal to "1"
Then use the copy constructor:
std::string test3(test1);
P.S. Don't use C-style casts. Use C++ style static_cast etc. This will make it easier to understand the program that you are writing.
Assuming you need to pass a const void* to a legacy C-style API, you need to use:
std::string test;
reinterpret_cast<const void*>(test.c_str());
C++ is not C.
What you show is close to the following C snippet:
char test1[] = "1";
const void* test2 = test1; // test1 decays to a pointer
char test3[sizeof(test1)];
strcpy(test3, test2);
But a C++ std::string is a complex container with internal data for housekeeping and an array of characters accessible through the data or c_str method:
std::string test1 = "1";
const void* test2 = test1.c_str();
std::string test3(static_cast<const char*>(test2), sizeof(test1));
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 am trying to create a specific case of a problem in my following code;
std::string id = "123456789123456789" // it can be different sized string too.
std::string my_id = "TEST_" + id;
char* temp_my_id = (char *) my_id.c_str();
strcpy(temp_my_id, my_id.c_str());
PRINT("my_id value : ", temp_my_id);
It is a legacy code in my project and I suspect the temp_my_id returns non-ascii character. Is this possible?
char* temp_my_id = (char *) my_id.c_str();
strcpy(temp_my_id, my_id.c_str());
source and destination can not have the same memory location in strcpy because both arguments are marked as restrict:
Bold test is mine:
restrict is a keyword (in C, not in C++) that can be used in pointer declarations. By
adding this type qualifier, a programmer hints to the compiler that
for the lifetime of the pointer, only the pointer itself or a value
directly derived from it (such as pointer + 1) will be used to access
the object to which it points.
you can try
char a[] = "123";
char *b = a;
strcpy(b, b);
returns:
warning: ‘strcpy’ source argument is the same as destination [-Wrestrict]
again in C, in C++ it is also broken because source and destination can not overlap.
strcpy does not really care about the encoding, it copies until it finds a NULL byte. So you just have to be careful that
the source ends with a zero byte and contains none in the middle
the source is not longer than the destination
source and destination should not overlap
see strcpy vs. memcpy and
http://www.cplusplus.com/reference/cstring/strcpy/
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 4 years ago.
Improve this question
I want to save a C String to a C++ Variable. This shall than be passed to the C-API function.
The string is defined in the C-API
as
#define XI_PRM_BINNING_HORIZONTAL_MODE "binning_horizontal_mode"
I want to select the string in a switch
char * binnMode;
...
binnMode = XI_PRM_BINNING_HORIZONTAL_MODE;
But that fails because the C-literal is defined as char str[] (C++11)
I want to pass the string to a function that expects
xiSetParamInt(IN HANDLE hDevice, const char* prm, const int val)
How do I need to define binnMode in order to get this working?
Notice that the parameter is const char*, not char*.
That's convenient because it's also what binnMode needs to be.
String literals are immutable, and (in C++) const. Yours is a const char[<length+1>] (don't know where you heard it was a char[]) which decays nicely to a const char*.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I don't have much experience in C++. I have a function std::string Exec(char* cmd). It runs for cout<<Exec("hello!"), but it doesn't run for std::string: invalid conversion from ‘const char*’ to ‘char*. I was wondering how I can fix it.
std::string s="hello";
char * c = s.c_str();
Exec(c);
Well that's because c_str() returns a const char * and not a char *
Just change the line to:
const char * c = s.c_str();
and the function declaration to
std::string Exec(const char* cmd)
Like noted before and you're good to go.
See it live here.
You need to change your prototype to this:
std::string Exec(const char* cmd)
since you want to pass a const char* as an argument.
Otherwise, you can pass a char* as an argument.
That's why your's cout<<Exec("hello!") works, because the argument is passed as non-const!
The correct signature for what you're attempting to do would be
std::string Exec(const char* cmd)
because in either cases you're passing a string constant to it. Even if the Exec("hello") case compiles, that doesn't mean it's safe to use:
#include <iostream>
using namespace std;
std::string Exec(char* cmd) {
cout << cmd;
cmd[0] = 'S'; // Undefined behavior
cout << cmd;
return std::string("");
}
int main() {
Exec("hello");
return 0;
}
http://ideone.com/ZY6Atr
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I see some code written like this:
char str[256] = {0};
or
char str[256] = {1};
when the former case, I use
printf("%s", str);
gives nothing,
Does it means give all the str[0] to str[256] all value 1?
when latterthe stdio give a ASCII smile char.
Furthermore, what is the difference between
char s[256] = {0};
printf("%c", s[1]);
It gives nothing in stdout
char s[256] = {0};
printf("%s", s[1]);
it give a (null)
I do not understand because I am a beginner of c char array and c pointers.
This initializes all 256 chars to 0:
char str[256] = {0};
This one initializes the first one to 1, and all the rest to 0:
char str[256] = {1};
Concerning the behaviour of printf, "%c" expects a single char. You pass it 0, which is NUL (the character string termination) so it prints nothing. "%s" expects a char* with the first character in a nul-terminated character string. You pass it a single char with value 0, which it interprets as a null pointer.