To expand a little on the other posts, a pointer is simply a variable which stores a memory address. This stored memory address is typically the location of a variable, but this is not always the case (you can set a pointer to 0 for example, and there is not valid object at the memory address 0).
When you compare two pointers, you compare the addresses which are stored in the two pointer variables and NOT the objects at those addresses.
int main(){ char* pchar = new char; if(array == "Hello") { //... }}
In the above code we have tried to compare a character pointer with "Hello". "Hello" is called a string literal. A string literal is a variable of type const char[], that is a const character array. In this example we have a string literal "Hello" which has a type of const char[6].
Arrays have the property that they can be implicitly converted into a pointer to the first element of the array. This means that you can use the name of the array as if it were a pointer which stores the memory location of the first element in the array. As an example, "Hello" can be implicitly converted to a const char pointer which contains the memory address of the character H.
With that in mind, it should be a bit clearer as to what is going on in the above example.
if(pChar == "Hello")
The above line is treated as a comparison between two pointers. The reason for this is that there is no operator== defined in C++ which compares a const char[6] to a char*. The only operator== that can be called is the operator== which has a prototype of
bool operator==(T* ParameterOne, T* ParameterTwo)
Where both parameters are of the same type. Since pChar is a char* and our string literal "Hello" is a const char[6] which has an implicit conversion to both const char* and char*, the operator== with the following prototype is called. #1
bool operator==(char* ParameterOne, char* ParamterTwo)
The above prototype, as mentioned before, simply compares the memory addresses stored in the two pointer variables. In the case of pChar, this will be the address returned from the call to new char. In the case of our string literal "Hello", the const char[6] will be implicitly converted to a pointer to the first character of the array, namely a pointer which stores the memory address of the letter H. Clearly the address of letter H and the address returned from our call to new char are different, and so the comparison returns false.
Your case is slightly different in that the pChar is pointing to a dynamic array rather than just a dynamic char.
#include<cstring>int main(){ char* pChar = new char[6]; std::strcpy(pChar, "Hello"); if(pChar == "Hello") { //.. }}
This is exactly the same as the previous example, as you are still comparing a variable of type char* with a const char[6], which can only result in the comparison operator for two pointers being called. Clearly pChar will point to the memory address returned from new char[6], which in this case also now happens to be the location of a character array containing the word Hello. Our string literal is still a const char[6] which has an implicit conversion to char* and so the operator== stated in the earlier example is called. It is important to realize that the const char[6] of the string literal and the char[6] returned from our call to new char[6] are completely different objects with completely different memory addresses, and so pointers to them can never be equal.
Clearly this pointer comparison is not what you intended, you merely wanted to compare the two arrays that the pointers point to, and not the addresses of there first elements. To do this for character arrays, there is a function called
strcmp.
The correct code would then be
#include<cstring>int main(){ char* pChar = new char[6]; std::strcpy(pChar, "Hello"); if(std::strcmp(pChar, "Hello") == 0) { //Two strings are equal }}
The functions strcpy and strcmp are inherited from C.
Now C++ provides a much neater way of going about this. The C++ standard Library contains a string object which encapsulates much of what you are trying to do. It also makes use of operator overloading to provide a more natural interface for dealing with strings. The above code, for example, can be replaced with the equivalent C++ code of
#include<string>int main(){ std::string One = "Hello"; if(One == "Hello") { //Two strings are equal }}
In the above code, the constructor for the std::string encapsulates the previous examples calls to new char[6] and to std::strcpy. The operator== for an std::string and a const char* is overloaded to encapsulate the call to std::strcmp.
The main advantages of using std::string instead of explicitly using strcpy and strcmp yourself are:
It handles all memory management for you - you do not have to worry about using new and delete to allocate the string, this is all handled for you.
It allows you to do the same thing in much fewer lines of code
It makes your code more readable
It makes you more productive
For a full list of the functions supported by std::string, you should consider buying a reference book like The C++ Standard Library by Josuttis.
#1 - (NOTE the conversion from const TYPE[] to TYPE* applies only to string literals, const int[6] cannot be implicitly converted to type int*, though you could convert it explicitly).