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

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.

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

C-Style Character String [duplicate]

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.

convert std::string to unsigned char [duplicate]

This question already has answers here:
How to convert a string of hex values to a string?
(4 answers)
Converting a hex string to a byte array
(22 answers)
Closed 4 years ago.
I have a string like this:
std::string s="840D8E88B0AC";
and an array:
char MAC[6];
I want to produce this:
MAC={0x84,0x0D,0x8E,0x88,0xB0,0xAC};
I try with sscanf() but I can't make it.
sscanf(s.c_str(), "%02X%02X%02X%02X%02X%02X", MAC[0], MAC[1], MAC[2], MAC[3], MAC[4], MAC[5]);
It should be (other errors notwithstanding)
sscanf(s.c_str(), "%02X%02X%02X%02X%02X%02X", &MAC[0], &MAC[1], &MAC[2],
&MAC[3], &MAC[4], &MAC[5]);
sscanf (and variants) require pointers in order to change the variables that are being read into.
Surprised your compiler didn't warn you about that error.

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

std::sort on a c-style string, without copying the string [duplicate]

This question already has answers here:
What is the difference between char s[] and char *s?
(14 answers)
Why do I get a segmentation fault when writing to a "char *s" initialized with a string literal, but not "char s[]"?
(19 answers)
Closed 5 years ago.
I've run into a strange, and very specific issue. As some background, I need to support some legacy code that's passing around a lot of large c-style strings. Since these get pretty gigantic, I want to avoid unnecessarily copying that data, which would include copy constructing std::strings just for one operation.
I want to use std::sort on these strings, but they obviously lack iterators. However, according to the documentation for std::sort, I should just be able to pass in two pointers for any contiguous random access memory, but it isn't working with my c-style string
Basically it's like this:
char* foo = "test string";
std::sort(foo, foo + 11); // access violation occurs
However:
char foo[] = "test string";
std::sort(foo, foo + 11); // perfect fine, sorted correctly
This is extra confusing to me, because as far as I know, char* and char[] are essentially the same. Why does char* break, while char[] succeeds? How can I pass a c-style string into an STL algorithm?
char* foo = "test string";
This is a pointer to a string literal, which is stored in read-only memory, thus you cannot modify/sort it.
Your code should be giving you a warning, similar to this:
Georgioss-MacBook-Pro:~ gsamaras$ g++ -Wall main.cpp
main.cpp:3:17: warning: conversion from string literal to 'char *' is deprecated
[-Wc++11-compat-deprecated-writable-strings]
char* foo = "test string";
^
Please read: What is the difference between char s[] and char *s?