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) {
...
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:
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.
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:
Using const char* as key for map/unordered_map
(3 answers)
Using char* as a key in std::map
(10 answers)
Closed 2 years ago.
So, I am trying to learn map in C++. I put the type as std:: map <const char *, int> to a variable month.
Say, the data is month["Jul"] = 7;
When I tried to retrieve the value directly
printf("%d", month["Jul"]);
It succeeds and outputs the value.
7
Trying with another variable succeeds too.
const char *p = "Jul";
printf("%d", month[p]); // 7
But strange behavior happens when I tried to assign it with a given input.
const char in_month[3];
scanf("%s", in_month);
print("%d". month[in_month]);
It retrieves (null)
0
Did I do something forbidden here or was there a step that I missed?
When you use const char* as the key, std::map will compare on the pointer directly, but not the content pointed by the pointer. For c-style string literals, the identical string literals might be compared as equal, but this is not guaranteed.
The compiler is allowed, but not required, to combine storage for equal or overlapping string literals. That means that identical string literals may or may not compare equal when compared by pointer.
When you use the array in_month, after converting to pointer it points to different things, then the pointer comparison result is false.
Better to use std::string instead as std:: map <std::string, int>. std::string has well-defined comparison operator which compares based on the content of string.
This question already has answers here:
Using the equality operator == to compare two strings for equality in C [duplicate]
(9 answers)
Closed 10 years ago.
I currently have a method called getResult which returns const char* now I am doing something like this
if(getResult=="Something")
{
...
}
However it seems this type of comparison doesn't work. Isn't "Something" also a const char pointer ? or in this case are the addresses being compared?
Yes, the addresses are being compared. To compare the contents of two strings, use the strcmp function:
if (strcmp(getResult, "Something") == 0) {
...
}
If getResult is a method (member function) you need to call it to compare the result, so you probably want:
if (something.getResult() == std::string("Something"))
// ...
If, on the other hand, getResult is really a pointer to char (or pointer to const char), you need to convert one of the things you're comparing to string before the comparison):
if (getResult == std::string("Something"))
or:
if (std::string(getResult) == "something))
IMO, this should almost never be necessary -- instead of starting with a pointer to [const] char, then converting, you should normally use std::string throughout.
If you compare 2 string constants in C/C++ you compare the actual address of each string, not the content. Using strcmp on 2 c-strings or using C++'s std::string class will compare the content.