简体   繁体   中英

determining True/False

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:))

If the result of the subtraction is zero, test will be false; otherwise it will be true.

Any value that is of a numeric type, enumeration type, or is a pointer can be converted to a boolean; if the value is zero (or null, for pointers), the result is false; otherwise the result is true.

Integer types, floating point types, and pointer types are all convertable to bool. For all of them, a value of 0 converts to false, and a non-zero value converts to true.

So, if s2 - s1 evaluates to 0, then test is false. Otherwise, test is true.

Since s1 and s2 are pointers, s2 - s1 is giving the difference between them (how far apart the addresses are). If they point to the same address, then the difference will be 0. If they point to different addresses, then the result will be non-zero. So, really all test indicates is whether s1 and s2 point to different addresses. s1 != s2 would give exactly the same result and would probably make more sense.

However, given that the values for s1 and s2 are hard-coded and are guaranteed to point to different addresses, the test doesn't really make any sense. It could make sense in another program though - particularly one where s1 and s2 are being passed into a function and you have no way of knowing ahead of time whether they're really the same.

The line bool test = s2-s1 does pointer subtraction, giving the number of char s separating s2 from s1 . This result is then converted to a bool by converting to false if and only if the result is 0, otherwise converting to true .

For a detailed description of what's going on when you add/subtract pointers, see: http://www.cs.umd.edu/class/spring2003/cmsc311/Notes/BitOp/pointer.html

C++ allows implicit conversions, so what it is doing is subtracting the pointer values of one array from another and if the result is null, then it is implicitly casting null to false. Anything else will be true, as bools work such that it's either zero (false) or true.

Thanks to Nathan S. and indiv for correcting me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM