determining True/False.

Posted by sil3nt on Stack Overflow See other posts from Stack Overflow or by sil3nt
Published on 2010-06-09T00:28:41Z Indexed on 2010/06/09 0:32 UTC
Read the original article Hit count: 292

Filed under:
|
|
|
|

Hi there, the following code

#include <iostream>
using namespace std;

int main(){
    char greeting[50] = "goodmorning everyone";
    char *s1 = greeting;
    char *s2 = &greeting[7];

    bool test = s2-s1;

    cout << "s1 is: " << s1 << endl;
    cout << "s2 is: " << s2 << endl;
    if (test == true ){
        cout << "test is true and is: " << test << endl;
    }

    if (test == false){
        cout<< "test is false and is: " << test << endl;
    }

    return 0;
}

outputs:

s1 is: goodmorning everyone
s2 is: ning everyone
test is true and is: 1

here what does the line bool test = s2-s1; actually evaluate?, is it the length of the string?. If so, then seeing as s2 is a smaller than s1 it should be negative correct?, and yet the output is true?.

Also if i change it to bool test = s1-s2; I still end up with the same result. So it doesnt matter whether its negative or positive the it will be true? and only false when 0?.

what does the s2-s1 mean?

-cheers (trying to get rid of doubts:))

© Stack Overflow or respective owner

Related posts about c++

Related posts about gcc