简体   繁体   English

如果在给定条件字符串的情况下将其视为true,则返回什么?

[英]If in if condition string is given it is treated as true but what it return?

Why exactly is a string literal in an if-condition treated as true? 为什么将if条件中的字符串文字准确地视为true?

if("whatiamreturning")
//this is true. I want to know y?

Based on the above, what happens here? 基于上述情况,这里会发生什么?

#‎include‬<stdio.h>
void main() {
    static int i;
    for(;;) { //infinite loop
        if(i+++"The Matrix")
            // what is happening in the above line?
            printf("Memento");
        else
            break;
    }
}

It will return the address of first element of the string whatiamreturning . 它将返回字符串whatiamreturning的第一个元素的地址。
Basically when you assign a string literal to a char pointer 基本上,当您将字符串文字分配给char指针时

char *p;
p = "whatiamreturning";

the assignment doesn't copy the the characters in whatiamreturning , instead it makes p point to the first character of the string and that's why string literals can be sub-scripted 赋值不会复制whatiamreturning的字符,而是使p指向字符串的第一个字符,这就是为什么字符串文字可以使用下whatiamreturning原因

char ch = "whatiamreturning"[1];

ch will will have character h now. ch现在将具有字符h This worked because compiler treated whatiamreturning as a char * and calculated the base address of the literal. 之所以whatiamreturning ,是因为编译器将whatiamreturning视为char *并计算了文字的基址。


if(i+++"The Matrix") is equivalent to if(i+++"The Matrix")等效于

if( i++ + "The Matrix")  

or it can be rewritten as 或者可以将其重写为

if(&("The Matrix"[i++]))    

which will be true for every i and results in an infinite loop. 这对于每个i都是true ,并导致无限循环。 Ultimately, the code will suffer from undefined behavior due to integer overflow for variable i . 最终,由于变量i整数溢出,代码将遭受不确定的行为

  if(i+++"The Matrix") // what is happening here please help here to understand 

This will take the value of i , add the pointer value of the location of the string "The Matrix" in memory and compare it to zero. 这将采用i的值,将字符串"The Matrix"在内存中的位置的指针值相加并将其与零进行比较。 After that it will increase the value of i by one. 之后,它将i的值增加一。

It's not very useful, since the pointer value could be basically any random number (it depends on architecture, OS, etc). 它不是很有用,因为指针值基本上可以是任何随机数(它取决于体系结构,操作系统等)。 And thus the whole program amounts to printing Memento a random number of times (likely the same number each run though). 这样,整个程序就等于随机打印了Memento次(尽管每次运行可能都相同)。

Perhaps you meant to write if(*(i+++"The Matrix")) . 也许您打算写if(*(i+++"The Matrix")) That would loop 10 times until it i+"The Matrix" evaluates to the address pointing to the NUL byte at the end of the string, and *(i+"The Matrix") will thus return 0 . 这将循环10次,直到i+"The Matrix"求出指向字符串末尾NUL字节的地址,然后*(i+"The Matrix")将返回0

Btw, spaces are a nice way to make your code more readable. 顺便说一句,空格是使代码更具可读性的好方法。

if("whatiamreturning") 

is equivalent to 相当于

if (1)

This is because "whatiamreturning" is a char [] that decays into a non-NULL char const* inside the if() . 这是因为"whatiamreturning"是一个char [] ,它会分解为if()内部的非NULL char const* Any non-NULL pointer evaluates to true in the context of a boolean expression. 在布尔表达式的上下文中,任何非NULL指针的计算结果都为true

The line 线

if(i+++"The Matrix") 

can be simplified to: 可以简化为:

if( (i++) + "The Matrix") 

In the first iteration of the loop, the value of i is 0 . 在循环的第一次迭代中, i值为0 Hence, the (i++) + "The Matrix" evaluates to "The Matrix" . 因此, (i++) + "The Matrix"计算结果为"The Matrix"

In the second iteration of the loop, the value of i is 1 . 在循环的第二次迭代中, i值为1 Hence, the (i++) + "The Matrix" evaluates to "he Matrix" . 因此, (i++) + "The Matrix"计算结果为"he Matrix"

However, the loop never ends and goes into the territory of undefined behavior since (i++) + "The Matrix" never evaluates to 0 and the value of i keeps on increasing. 但是,循环永远不会结束并且进入未定义行为的领域,因为(i++) + "The Matrix"永远不会求值为0并且i的值会不断增加。

Perhaps they meant to use: 也许他们打算使用:

  if(i++["The Matrix"])

which will allow the expression inside if() it to be 0 after 10 iterations. 经过10次迭代后, if()的表达式将变为0

Update 更新资料

If you are following somebody else's code, stay away anything else that they have written. 如果您遵循别人的代码,请远离他们写的其他任何东西。 The main function can be cleaned up to: main功能可以清除为:

int main() {
    char name[] = "The Matrix";
    int i = 0;
    for( ; name[i] != '\0'; ++i )
    {
        printf("Memento\n");
    }
}

Why exactly is a string literal in an if-condition treated as true? 为什么将if条件中的字符串文字准确地视为true?

 if("whatiamreturning") 

The string literal "whatiamreturning" is a constant of type char[] . 字符串文字"whatiamreturning"char[]类型的常量。
In nearly all contexts, including this one, arrays decay to pointers to their first element. 在几乎所有上下文(包括该上下文)中,数组都会衰减到指向其第一个元素的指针。
In a boolean context, like the condition of an if -statement, all non-zero values are true. 在布尔上下文,等的条件下if语句来,所有的非零值都为真。
As the pointer points to an object, it is not the null-pointer, and thus is true. 当指针指向对象时,它不是空指针,因此为true。

Based on the above, what happens here? 基于上述情况,这里会发生什么?

 #‎include‬<stdio.h> void main() { 

The above is your first instance of Undefined Behavior , whatever happens, it is right. 上面是您的Undefined Behavior的第一个实例,无论发生什么,都是正确的。
We will now pretend the error is corrected by substituting int for void . 现在,我们将用int代替void来纠正错误。

Now, your loop: 现在,您的循环:

    static int i;

Static variables are default initialized, so i starts with value 0. 静态变量是默认初始化的,所以i从0开始。

    for(;;) { //infinite loop
        if(i+++"The Matrix")
            // what is happening in the above line?
            printf("Memento");
        else
            break;
    }

This loop has Undefined Behavior as well. 此循环也具有未定义的行为

The condition takes i and adds it to the string literal "Memento" which decayed to a pointer like in the previous example, interpreting the resultant pointer in a boolean context, and as a side-effect incrementing i . 该条件采用i并将其添加到字符串文字"Memento" ,该字符串文字像前面的示例一样衰减为指针,在布尔上下文中解释结果指针,并作为副作用使i递增。
As long as i is no more than strlen("The Matrix")+1 on entry, everything is ok, the pointer points to an element of the string literal or one past, and the standard guarantees that's not a null pointer. 只要i在输入时不超过strlen("The Matrix")+1 ,一切就可以了,指针指向字符串文字的一个元素或一个过去,并且该标准保证该指针不是空指针。
The moment it is though, all hell breaks loose because calculating such a pointer is Undefined Behavior . 然而,由于计算这样的指针是Undefined Behavior ,所以一切都变得混乱了

Well, now that we know the loop is UB, let's ignore the loop too. 好了,既然我们知道该循环是UB,那么让我们也忽略该循环。 The rest of the program is: 该程序的其余部分是:

}

Which is ok, because even though main has a return type of int , there's a special rule which states that if control reaches the end of main without executing a return -statement, an implicit return 0; 没关系,因为即使main的返回类型为int ,也有一条特殊的规则指出,如果控制到达main的末尾而不执行return -statement,则隐式return 0;否则, return 0; is added. 被添加。

Side-note: If an execution of a program encounters Undefined Behavior anywhere, the whole program is undefined, not only from that point on: Undefined behavior can result in time travel (among other things, but time travel is the funkiest) 旁注:如果某个程序的执行在任何地方都遇到“未定义行为”,则整个程序都是未定义的,不仅是从那时起: 未定义行为会导致时间旅行(除其他外,时间旅行是最有趣的)

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

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