October 18th, 2013, 02:31 AM
Using pointers in if statements
I want to pass character value from main to class function.
In the class function the value is taken by the char*a pointer.
inside the function i wrote as
but even if i pass value xxx to the pointer a, it displays pqr on the screen instead of abc. please help me resolve this?
October 18th, 2013, 03:56 AM
You can not compare strings by their pointers (i.e. memory addresses) - use "strcmp" function instead.
October 18th, 2013, 04:28 AM
Alternatively you can use string class:
if (string(a) == string("xxx"))
October 18th, 2013, 04:35 AM
Originally Posted by rodiongork
yeah I used it and it worked.thnks
October 18th, 2013, 11:28 AM
It works now, but did you learn why?
a is an array name, so it acts like a pointer in almost all situations (you just cannot assign a new address to it). "xxx" is a string literal and is used handled in code as a char pointer.
Therefore, what (a=="xxx") does is that it compares two pointers, not the values that are being pointed to, but rather the addresses of that data. That's what pointers are and do. Since the array a is not at the same memory location of the string literal "xxx", their addresses can never be the same and therefore that condition will always be false regardless of what string you store at a.
BTW, the same thing would happen if you tried to assign one string to another. Instead of creating a copy of the string, you would just set the pointer on the left to point to the same string. And obviously, if you tried to assign "xxx" to the char array a, then you would get a compiler error since you'd be trying to change the address of a, which you cannot do.
Read and write your code with an understanding of what it actually means, just as you would with any human language that you know.
To be able to compare the contents of two C-style strings (ie, char arrays with null-terminators), the standard library provides some string functions: strcmp, stricmp, strncmp.
Since you are using C++ (as evidenced by the cout<<), you could also use the basic string class, as was also suggested. One of the many advantages of the basic string class is that it overloads many operators, such as assignment and equality. Overloading is a very handy feature of C++ in which several different versions of the same function or operator can be created in order to work with different argument and operand datatypes. Operator overloading is usually, if not always, associated with a class; that is how the << operator of the cout object can output all kinds of datatypes correctly.
The basic string class overloads the == relational operator in order to redefine it to compare the contents of strings, as well as to be able to handle comparing a string object to another string object or to a C-style string. Hence, you could write:
Please note that this will work only if a is a basic string object and not if it's a C-style string. Please think about that and re-read as much as you need to until you understand why that is.
cout<<"abc"; // since a does equal "xxx", this will print out
cout<<"pqr"; // this will not print out
BTW, I think that it is good that you are also learning C-style strings. Current C++ books and courses never teach it nor C-style I/O (ie, scanf, printf), thus leaving two big holes in the students' knowledge. You will encounter code that uses C-style strings, so you do need to know about them.