简体   繁体   English

确定是非

[英]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; 这里的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?. 如果是这样,则看到s2小于s1,它应该为负正确吗?,而输出为true。

Also if i change it to bool test = s1-s2; 另外,如果我将其更改为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 ? 因此,不管它的消极还是积极的事实都是true and only false when 0?. 并且只有0时为false。

what does the s2-s1 mean? s2-s1是什么意思?

-cheers (trying to get rid of doubts:)) 欢呼(努力摆脱疑虑:)

If the result of the subtraction is zero, test will be false; 如果相减的结果为零,则test为假;否则为零。 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; 如果值为零(对于指针,则为null),则结果为false;否则,结果为false。 otherwise the result is true. 否则结果为true。

Integer types, floating point types, and pointer types are all convertable to bool. 整数类型,浮点类型和指针类型都可以转换为bool。 For all of them, a value of 0 converts to false, and a non-zero value converts to true. 对于所有这些,值0会转换为false,而非零值会转换为true。

So, if s2 - s1 evaluates to 0, then test is false. 因此,如果s2 - s1评估为0,则test为假。 Otherwise, test is true. 否则, test为真。

Since s1 and s2 are pointers, s2 - s1 is giving the difference between them (how far apart the addresses are). 由于s1s2是指针,因此s2 - s1给出它们之间的差(地址有多远)。 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. 如果它们指向相同的地址,则差将为0。如果它们指向不同的地址,则结果将为非零。 So, really all test indicates is whether s1 and s2 point to different addresses. 因此,实际上所有test表明s1s2是否指向不同的地址。 s1 != s2 would give exactly the same result and would probably make more sense. s1 != s2将给出完全相同的结果,并且可能更有意义。

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. 但是,鉴于s1s2的值是硬编码的,并且保证指向不同的地址,因此该测试实际上没有任何意义。 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. 不过,在另一程序中可能会有意义-尤其是将s1s2传递到函数中的程序,您无法提前知道它们是否真的相同。

The line bool test = s2-s1 does pointer subtraction, giving the number of char s separating s2 from s1 . bool test = s2-s1指针减法,给出将s2s1分开的char s的数量。 This result is then converted to a bool by converting to false if and only if the result is 0, otherwise converting to true . 然后,仅当结果为0时,才将结果转换为false从而将该结果转换为bool,否则将其转换为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 有关添加/减去指针时发生的情况的详细说明,请参见: 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. C ++允许隐式转换,因此它正在做的是从另一个数组中减去一个数组的指针值,如果结果为null,则它隐式将null转换为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. 感谢Nathan S.和indiv纠正我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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