#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    2
    Rep Power
    0

    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
    if (a=="xxx")
    cout<<"abc";
    else
    cout<<"pqr";


    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?
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Location
    Saint-Petersburg, Russia
    Posts
    236
    Rep Power
    28
    Hi!

    You can not compare strings by their pointers (i.e. memory addresses) - use "strcmp" function instead.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2009
    Posts
    45
    Rep Power
    8
    Alternatively you can use string class:
    Code:
    #include <string>
    
    if (string(a) == string("xxx"))
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    2
    Rep Power
    0
    Originally Posted by rodiongork
    Hi!

    You can not compare strings by their pointers (i.e. memory addresses) - use "strcmp" function instead.

    yeah I used it and it worked.thnks
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,163
    Rep Power
    2222
    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:
    Code:
        string a("xxx");
    
        if (a=="xxx")
            cout<<"abc";  // since a does equal "xxx", this will print out
        else
            cout<<"pqr";  // this will not print out
    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.

    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.

IMN logo majestic logo threadwatch logo seochat tools logo