C-Style Character String [duplicate] - c++

This question already has answers here:
Concatenate char arrays in C++
(7 answers)
C++ concatenate two int arrays into one larger array
(6 answers)
Fastest way to append two arrays ( concatenate two arrays) C++
(7 answers)
Closed 8 months ago.
I want to connect two C-style character strings and store the result in a dynamic char array.
int main()
{
char word1[] = "hello";
char word2[] = "haha";
auto ptr = new char[20];
strcpy(ptr,strcat(word1,word2));
cout<<ptr<<endl;
return 0;
}
The compiler says there is a "segmentation fault" at the statement strcpy(ptr,strcat(word1,word2));. Why does the compiler say that?

Like this
strcpy(ptr, word1);
strcat(ptr, word2);
You need to use ptr for both operations, the copy and the concatenation.
In your version strcat(word1,word2) tries to concatenate word2after the end of word1. But there is no accessible memory there, so you get a segmentation fault.

Related

should the value of pointer always be an address? [duplicate]

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";

error in concatenating the strings in C++ [duplicate]

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.

fastest way to compare char* (C/C++)? [duplicate]

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) {
...

Strcat() returns garbage after strcpy [duplicate]

This question already has answers here:
functions returning char pointer
(12 answers)
Closed 7 years ago.
Why this does not work? With strcat commented it returns first char * OK, but with strcat uncommented i get garbage characters.
char * concat(char* first, char* second){
char result[10]; // array to hold the result.
strcpy(result,first); // copy string one into the result.
strcat(result,second); // append string two to the result.
return result;
}
concat(rPlayer.name,"blbost");
You're returning an address to first element of an array which is local to the function which is no longer valid(/exists) after the function returns.
The first should have the contents of the second array appended to it. You need to be sure that the first has enough space already allocated to append all of the characters from the second array so as not to run into undefined behavior.

array vs pointer of char type [duplicate]

This question already has answers here:
Why does cout print char arrays differently from other arrays?
(4 answers)
Closed 8 years ago.
char label[] = "Single";
char *labelPtr;
labelPtr = label;
cout<<labelPtr;;
return 0;
The ouput with the above code is Single.
I'am confused about the output.
As far as my understanding labelPtr should contain the address of label[0]. So shouldn't the output be equivalent to &label[0]?
std::cout treats char* as a pointer to a C-style string.
So the whole C-style string is printed.
Consider casting to (void*) to see what will happen.
labelPtr = label;
In this label refers to the first element of the char array. And then you are making labelptr point to that same array. That means from now onwards you can use labelptr in a same way you use label.
So, both are equivalent:-
cout << label;
cout << labelptr;
As cout considers the argument to be of type char*. All the contents of the string is printed.
For you to print only the first character try using
cout<<*labelPtr;;
This would print only the first character. To print 2nd character use cout<< *(labelPtr+1);;