简体   繁体   English

逗号表达式的左侧操作数无效

[英]left-hand operand of comma expression has no effect

for (count = index, packet_no = 0; 
     count < TOTAL_OBJ, packet_no < TOTAL_PKT; 
     count++, packet_no++)

=> left-hand operand of comma expression has no effect. =>逗号表达式的左侧操作数无效。

I find the above code is correct and could not understand why this error comes.我发现上面的代码是正确的,但不明白为什么会出现这个错误。

This is how the comma operator works, what you want to do is to use OR or AND (probably AND in your case):这就是逗号运算符的工作方式,您想要做的是使用ORAND (在您的情况下可能是 AND):

// the condition for resuming the loop is that one of the conditions is true
count < TOTAL_OBJ || packet_no < TOTAL_PKT
// the condition for resuming the loop is that both conditions are true
count < TOTAL_OBJ && packet_no < TOTAL_PKT

You have three comma operators in each of the three terms of the for statement.for语句的三个术语中的每一个中都有三个逗号运算符。 The warning is for term 2.警告是针对第 2 学期的。

Both expressions of terms 1 and 3 are executed as expected.第 1 项和第 3 项的表达式都按预期执行。

The left operation of the term 2 is evaluated only as a void, doesn't take part in the for condition, and therefore leads to your warning.术语 2 的左操作仅被评估为空值,不参与for条件,因此会导致您发出警告。

The conditional statement(for, while or if ) having conditional expressions with comma operator(s), the value of last expression is the conditional value(True or False) of conditional statement.条件语句(for、while 或 if )具有带逗号运算符的条件表达式,最后一个表达式的值是条件语句的条件值(真或假)。 For ex.例如。

int i = 1;
int j = 0;
int k = 1;
if(i, j, k) {
   printf("Inside");
}else {
   printf("Outside");
}

prints "Outside" as comma operator is evaluated from left to right and k is the last expression evaluated in if statement which returns false.打印“外部”作为逗号运算符从左到右计算,k 是 if 语句中计算的最后一个表达式,它返回 false。

int i, j;
if(i = 0 , j = 1) {
   printf("Inside");
}else {
   printf("Outside");
}

Above prints "Inside".上面印有“内部”。 j = 1 is the last expression evaluated inside the if statement which has true value. j = 1 是 if 语句中最后一个具有真值的表达式。

int i = 1;
int j = 0;
int k = 1;
if(i, j, k) {
   printf("Inside");
}else {
   printf("Outside");
}

Correction to above: This code will print "Inside" as the comma operator is evaluated from left to right and k is the last expression evaluated in the if statement, which returns true since k = 1.更正上述内容:此代码将打印“Inside”,因为逗号运算符从左到右计算,k 是 if 语句中计算的最后一个表达式,由于 k = 1,因此返回 true。

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

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