开发者

Is this a valid C command/instruction?

开发者 https://www.devze.com 2023-02-14 17:15 出处:网络
I am looking over some code someone else did and I see this: if (numDetects == 0) { Table[Index].minF =

I am looking over some code someone else did and I see this:

            if (numDetects == 0) {

                Table[Index].minF = 

            Table[Index].maxF = F;

            }

The Table[Index].minF = blank does not make any sense to me. I've never seen this in my life. BUT the code does compile and run, so 开发者_如何学Ccould someone explain to me if this is possible or not to just have a equal sign left hanging there? Thanks!


Yes; C doesn't care about the white space between the first line and the second, so it sees it as

Table[Index].minF = Table[Index].maxF = F;

It's syntactically equivalent to

Table[Index].minF = (Table[Index].maxF = F);

since the assignment operator = not only assigns the left-hand side to the right-hand side, but also returns the value that was assigned. In this case, that return value is then in turn assigned to the outer left-hand side.


Yes, this is the same as:

Table[Index].minF = Table[Index].maxF = F;

The assignment operator (=) can be chained just like any other operator. It is evaluated from right to left, and each evaluation returns the value that was assigned. So this is equivalent to the following two statements.

Table[Index].maxF = F;
Table[Index].minF = Table[Index].maxF;


White space isn't important. The line really reads

Table[Index].minF = Table[Index].maxF = F;

Which is equivalent to

int a;
int b;

a = b = 0;


It is equivalent to:

Table[Index].minF = Table[Index].maxF = F;


The whitespace is ignored and its all evaluated as...

Table[Index].minF = Table[Index].maxF = F;

0

精彩评论

暂无评论...
验证码 换一张
取 消