This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Modify a char* string in C
I have:
char* htmlstring = "htmldata"
How do I extract:
char* htmlstring1 = "htmldata before </body>tag"
char* htmlstring2 ="htmldata after and including</body> tag"
Can I use strstr? Whats the best approach?
strncpy(destination_string, source + start, count)
It's not really safe, but works. You should wrap it around.
Related
This question already has answers here:
initializing char pointer as string vs other type pointers as arrays
(5 answers)
Initializing a char pointer C++ [duplicate]
(3 answers)
What is the type of string literals in C and C++?
(4 answers)
Closed 5 months ago.
so ive just begun learning about pointer basics and ive come across something im stuck on.
as the title says, should the value of the pointer must always be an address?
because i saw a line of code, which says otherwise:
char *text = "text";
this here is being used for the creation of a string, the other method is:
char text[] = "text";
which is pretty understandable.
could you guys explain to me what this line does exactly?
char *text = "text";
a pointer is being used but what does it do and point to? how can you use it to then access
the string created.
thanks.
"text" is a string literal. It is stored somewhere in memory and its address is used to initialise the pointer. You access the string as you would with any other pointer.
And as stated above
char *text = "text";
is not legal C++ (it is legal C) the correct C++ is
const char *text = "text";
This question already has answers here:
C++ concat three char* strings togther [closed]
(4 answers)
Closed 2 years ago.
When I am trying to run this code. I'm getting this error "C2110: '+' : cannot add two pointers". Can anyone just tell me what is wrong in the code?
string Msg;
getline(cin, Msg);
string output;
output = "<Rvc>\n"+"<Msg>"+Msg+"< / Msg>\n";
C-style string literals are not std::strings. "<Rvc>\n" and "<Msg>" are of type const char[] and could decay to pointers (i.e. const char*). Adding on pointers doesn't make sense.
You can just
output = "<Rvc>\n<Msg>"+Msg+"< / Msg>\n";
Then the overloaded operator+ for std::string taking const char* and std::string will be used.
This question already has answers here:
QString to char* conversion
(10 answers)
Closed 3 years ago.
I use in my project QString but In some cases I have to transfer this to an external SDK, that expect null terminated const char*. For this I use:
QByteArray folderName = ((QDataItem*)(*it))->GetName().toUtf8();
folderName.append('\0');
dir.lpszDir = folderName;
Is casting the right way here? I assume I have to cast it with dynamic cast? Or is there a better way to bring the "GetName()" to a const char*?
I did it like that in my project.
QString stringMessage = "something";
char* charMessage = new char[stringMessage.size() + 1];
std::memcpy(charMessage, stringMessage.toLocal8Bit().data(), static_cast<size_t>(stringMessage.size() + 1));
This question already has answers here:
C or C++. How to compare two strings given char * pointers?
(8 answers)
Closed 4 years ago.
If I have a char* that is the output of another function, and I know it is one of the 10 known words, what is the best way to find what is it?
converting the char* to string bystd::string(char*) , then using string.compare() ?
char* c = "hi";
string s = std::string(c);
if (s.compare("hello") )
Is this the best way? I can not directly write:
char* c ="hi";
if(c == "hello")
Since you already have a C string, just use strcmp. It will likely be faster than the s.compare method since you avoid the overhead of doing a conversion to std::string for both the original string and the string to compare to.
if (strcmp(c, "hello") == 0) {
...
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Convert char array to single int?
How to convert char array to uintmax_t?
char array contains the uintMax_t value but in string format.
Thank you.
You need to use atoi or atol .